简体   繁体   中英

Setting the Date in a node-http-proxy request

I am trying to create a node-http-proxy server that explicitly sets the Date passed in all requests. Can anyone please advise me how to do this.

ie I want to force the date passed in all HTTP request(s) to be a date in the past

Setting the Date in the req.headers does not appear to work:

var http = require('http'),
    httpProxy = require('http-proxy');

var proxy = httpProxy.createProxyServer({});
var server = http.createServer(function(req, res) {

  console.log("Proxying: " + req.url);
  req.headers["Date"] = "Fri, 18 Dec 2015 08:49:37 GMT";
  //req.headers["date"] = "Fri, 18 Dec 2015 08:49:37 GMT";
  proxy.web(req, res, { target: 'http://somewhereElse.com:9090' });
});

console.log("listening on port 8888")
server.listen(8888);

Look at this link .

So in your case:

var http = require('http'),
    httpProxy = require('http-proxy');

//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createProxyServer({});

// To modify the proxy connection before data is sent, you can listen
// for the 'proxyReq' event. When the event is fired, you will receive
// the following arguments:
// (http.ClientRequest proxyReq, http.IncomingMessage req,
//  http.ServerResponse res, Object options). This mechanism is useful when
// you need to modify the proxy request before the proxy connection
// is made to the target.
//
proxy.on('proxyReq', function(proxyReq, req, res, options) {
  proxyReq.setHeader('Date', 'Fri, 18 Dec 2015 08:49:37 GMT');
});

var server = http.createServer(function(req, res) {
  // You can define here your custom logic to handle the request
  // and then proxy the request.
  proxy.web(req, res, {
    target: 'http://127.0.0.1:9090'
  });
});

console.log("listening on port 8888")
server.listen(8888);

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