how to set proxy by request hostname with http-proxy-middleware and express?

Rife

I want to configure proxy with http-proxy-middleware and express . The rule is a mapping of hostname, exmaple:

http://123.com   >>  http://localhost:3000/123
http://456.com   >>  http://localhost:3000/abc

I have tried like this:

import express from 'express';
import http from 'http';
import proxy from 'http-proxy-middleware';

const app = express();

app.use( async function (req, res) {  
  let direction = 'http://localhost:3000';
  console.log('hostname: ', req.hostname);
  console.log('originalUrl: ', req.originalUrl);
  if (req.hostname == '123.com') {
    direction = `http://localhost:3000/123${req.originalUrl}`;
  }
  if (req.hostname == '456.com') {
    direction = `http://localhost:3000/abc${req.originalUrl}`;
  }

  return await proxy({ target: direction, changeOrigin: false })
});

const server = http.createServer(app);
app.set('port', '127.0.0.1');
server.listen(9999, '0.0.0.0');

but it doesn't work.

eol

There's a couple of things you need to consider:

  • The http-proxy-middleware module does not return a promise, instead it returns an express middleware.
  • You can use a custom filter to decide whether or not to proxy the request.
  • You need to add the pathRewrite options in order to rewrite the url according to the current hostname.
  • Another option would be to use the router option. See the relevant documentation.

I wrote a quick express app to test this (note that i overwrote my hosts file with localwebapp and localwebapp2 pointing to 127.0.0.1) and it seems to work fine:

const express = require('express')
const proxy = require('http-proxy-middleware')

const app = express();
const filter = (pathname, req) => {
    if (req.hostname == 'localwebapp' || req.hostname == 'localwebapp2') {
        return true;
    }
    return false;
};

app.get('/123*', (req, res) => {
    res.send(`matched 123* route: ${req.path}`);
})

app.get('/abc*', (req, res) => {
    res.send(`matched abc* route: ${req.path}`);
})

app.get('/test', (req, res) => {
    res.send("matched non proxied route '/test'");
})

const apiProxy = proxy(filter, {
    target: 'http://localhost:3000',
    logLevel: 'debug',
    changeOrigin: true,
    pathRewrite: function (path, req) {
        if (req.hostname == 'localwebapp') {
            return `/123${req.originalUrl}`;
        }
        if (req.hostname == 'localwebapp2') {
            return `/abc${req.originalUrl}`;
        }
        return path;
    }
})
app.use(apiProxy)
app.listen(3000);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to proxy to many different targets using http-proxy-middleware?

How to set up a node http proxy to intercept a particular request/response?

How to set Http Proxy in an applet

How to make a bocking Request in NodeJS inside a http-proxy-middleware function?

http-proxy-middleware: return custom error instead of proxying the request

http-proxy-middleware econnreset error after successful post request

Specifying proxy for http-proxy-middleware

How to set proxy host on HttpClient request in Java

How to set proxy for each request in OkHttp?

How to set http-request node not to use environment set proxy settings?

How to set an HTTP proxy in Python 2.7?

Node - express-http-proxy - set Header before proxying

how to set default proxy with .NET core 3.1 for HTTP client for any request?

How to set proxy_http_version in LUA code before upstreaming the request in nginx

Form parameters are not passed to the express app while using http-proxy-middleware

How to proxy/stream HTTPS request using Express/got?

http-proxy-middleware - access to the static files

Handle WebSocket error with http-proxy-middleware

Rewriting path with http-proxy-middleware

Custom response with http-proxy-middleware package

Websockets not working with http-proxy-middleware

Use proxy for http request in Eclipse

GoColly set proxy for every request

How to modify response headers with express-http-proxy

How to Proxy using Node using express-http-proxy to strip final rest

Selenium - Set http proxy to the webdriver

http_proxy is set but where?

How to set a proxy for terminal?

How to tell if a GET request in Python is using an https or http proxy?