Forward express js route to other server

前端 未结 1 829
面向向阳花
面向向阳花 2021-01-05 07:30

I\'m writing an express.js app and would like to serve certain routes from another server.

Express serves some routes starting with api.example.com/v1/*

1条回答
  •  伪装坚强ぢ
    2021-01-05 08:25

    You can use express-http-proxy.

    And yes, proxying is the accurate term for what you are looking for. It will underneath forward the request to another url, as the API will also imply.

    Here's an example usage:

    var proxy = require('express-http-proxy');
    var app = require('express')();
    
    app.use('/route/you/want/proxied', proxy('proxyHostHere', {
        forwardPath: function (req, res) {
          return '/path/where/you/want/to/proxy/' + req.url
        }
    }))
    

    0 讨论(0)
提交回复
热议问题