I have a nginx server as a front end to a node.js application.
When the request gets to the application, I'd like to slightly modify it (the request) and forward it to another node.js application. What would be the best way to do this ?
I was thinking of node-proxy but as I use expressjs in the node apps, I'm not really sure how to use node-proxy and express at the same time.
any idea ?
UPDATE
Could I use res.redirect in my expressjs routes to forward to other node.js application ? I have just tried this but it does not work as expected.
I use node-http-proxy and express.js at the same time quite successfully. Here's the coffeescript source.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With