在node-http-proxy请求中设置Date

时间:2016-03-16 10:08:02

标签: node.js node-http-proxy

我正在尝试创建一个node-http-proxy服务器,该服务器显式设置所有请求中传递的Date。任何人都可以告诉我如何做到这一点。

即。我想强制所有HTTP请求中传递的日期为过去的日期

在req.headers中设置日期似乎不起作用:

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);

1 个答案:

答案 0 :(得分:1)

看看这个link

所以在你的情况下:

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);