node.js/express中的代理请求

Luc*_*Luc 2 proxy node.js

我有一个nginx服务器作为node.js应用程序的前端.
当请求到达应用程序时,我想稍微修改它(请求)并将其转发到另一个node.js应用程序.最好的方法是什么?

我在考虑节点代理,但是当我在节点应用程序中使用expressjs时,我不确定如何使用node-proxy和同时表达.

任何的想法 ?

UPDATE

我可以在expressjs路由中使用res.redirect转发到其他node.js应用程序吗?我刚试过这个,但它没有按预期工作.

Pet*_*ons 7

我使用node-http-proxy和express.js非常成功.这是coffeescript的来源.

querystring = require 'querystring'
httpProxy = require 'http-proxy'

#Your express setup code would be here
#omitted for brevity....

proxy = new httpProxy.HttpProxy()

#1. Whatever HTTP Methods and URL paths you want to modify and forward
app.all '/foo/*', (req, res) ->
  #2. Your logic to modify the request goes here
  #Note there are limitations to what you can do.
  #I add some extra query parameters to the URL
  query = if '?' in req.url then '&' else '?'
  params =
    extra1: 'foo'
    extra2: 'bar'
  req.url = [
    req.url
    query
    querystring.stringify params
  ].join ''
  #3. The host and port could also be pulled from the req object if needed
  proxy.proxyRequest req, res,
    host: 'somehost.example'
    port: 80
Run Code Online (Sandbox Code Playgroud)