我在 Node 中组装了一个代理服务器,它需要能够通过 tls 传输 https 请求并且一切正常。使用以下两个包非常容易设置:proxy、https-proxy-agent。我的问题是我正在尝试使用connect作为中间件层来捕获 HAR 文件,但出现以下错误:
_http_outgoing.js:357
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:357:11)
at ServerResponse.writeHead (_http_server.js:180:21)
at ClientRequest.<anonymous> (/.../node_modules/proxy/proxy.js:233:11)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:473:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
at Socket.socketOnData (_http_client.js:362:20)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
如下所示,它似乎只发生在我通过本地浏览器使用连接和代理时(此代理实际上与BrowserStackLocal一起使用)。当我从本地机器浏览器以外的任何地方通过代理时,就好像它甚至不知道中间件的存在。
所以基本上,我只需要让连接在这种情况下工作,我不确定我是否需要暂停和恢复,或者什么......任何想法将不胜感激。基本代码如下:
const path = require('path');
const http = require('http');
const proxy = require('proxy');
const Agent = require('https-proxy-agent');
const connect = require('connect');
const har = require('./har');
const middleware = connect();
middleware.use(har({
harOutputDir: path.resolve(process.cwd(), 'har/')
}));
const server = proxy(http.createServer(middleware));
server.agent = new Agent('http://localhost:8081');
server.listen(8081)
谢谢!
编辑:请注意: har 中间件根本不修改标头。