简体   繁体   中英

Replicate Nginx proxy_pass on NodeJs + Express

If you do this in Nginx:

location /apiproxy/ {
    proxy_pass                      https://apidev.site.com/;
}

All request to domain.com/apiproxy/* will be passed to apidev.site.com/*, so domain.com/apiproxy/foo/bar/ will be passed to apidev.site.com/foo/bar.

Im trying to replicate the same behavior in NodeJS with express and proxy , according to documentation:

app.use('/apiproxy/*', proxy('https://apidev.site.com/v1', {
    proxyReqPathResolver: function(req) {
        console.log(require('url').parse(req.url).path);
        return require('url').parse(req.url).path;
    }}));

But that loose everithing after /apriproxy/. Any idea?

Dont know if you have tried this, but if you do not add *. then it passes the url.

Example

app.use('/proxy', proxy('http://google.com', {
  proxyReqPathResolver: function (req) {
    console.log(require('url').parse(req.url).path);
    return require('url').parse(req.url).path;
  }
}));

If we call /proxy/abc , then it calls http://google.com/abc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM