Domanda

I am building a web application using node.js and I need to proxy certain routes to another Node.js (express) server

I have the below code

var express = require('express'),
    http=require('http'),
    httpProxy = require('http-proxy'),
    proxy = new httpProxy.RoutingProxy();


var server = express();
server.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
server.use(express.logger());
server.use(express.cookieParser());
server.use(express.query());
server.use(express.session({secret:"secret-key", maxAge:10*60*1000}));
server.use(routeRequest);

var routeRequest = function (req, res, next) {
 req.url=req.url.substring(1);
 if (req.url.indexOf("client")==0) {
    proxyToClientServer(9001,req,res)     
 }else{
    res.writeHead(404, { 'Content-Type': 'text/plain' });
    res.end();
  }


}

var proxyToClientServer=function(port, req, res){

 req.headers["data_context"]='customer_details';

 proxy.proxyRequest(req, res, {
    host: 'localhost',
    port: port,
    headers:req.headers
  })
}
server.listen(8000);

My problem is the request gets routed properly, but the data context (custom header) i added is removed from the request at the target server

Can somebody please advice how to pass more information to the server thats is being proxied

È stato utile?

Soluzione

As robertklep mentioned, the code was working , I was looking at the wrong place.

But one thing to remember is that we could only add Strings. Any objects I added was gone but I had to JSON.stringify(obj) and add to header to make it go through.

Objects that could not be stringified (like socket handles) could not be passed along.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top