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

我正在尝试创build一个节点http代理服务器,明确设置所有请求中传递的date。 任何人都可以请教我如何做到这一点。

即我想强制所有的HTTP请求传递的date是过去的date

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

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

看看这个链接 。

所以在你的情况下:

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