【发布时间】:2015-09-03 15:39:23
【问题描述】:
我正在编写 Express.js 应用程序,它应该将请求路由到某个代理服务器,作为管道中的最后一步。我使用了http-proxy 代理库,因为它支持 websockets。问题是我需要在重定向请求后继续使用我的应用程序,以便收集一些我将用于记录的信息(api 调用的响应时间等)。想法是在请求被代理后调用next()函数,这应该让我回到堆栈中的第一个中间件?(如果我错了请纠正我)然后计算起点和当前点之间的时间差.
在调用proxy.web(req, res, {target: serviceAddress}); 和next()之后,我得到了错误:
_http_outgoing.js:335
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:335:11)
at d:\WebStorm Projects\api-gateway\node_modules\http-proxy\lib\http-proxy\passes\web-outgoing.js:85:11
at Array.forEach (native)
at Array.writeHeaders (d:\WebStorm Projects\api-gateway\node_modules\http-proxy\lib\http-proxy\passes\web-outgoing.js:84:35)
at ClientRequest.<anonymous> (d:\WebStorm Projects\api-gateway\node_modules\http-proxy\lib\http-proxy\passes\web-incoming.js:149:20)
at ClientRequest.emit (events.js:107:17)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:426:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)
at Socket.socketOnData (_http_client.js:317:20)
at Socket.emit (events.js:107:17)
在调用proxy.web(...) 后是否有继续工作的解决方案?或者如果我的方法是错误的,有人可以建议我解决这个问题吗?
【问题讨论】:
-
我没有使用过这个代理模块。听起来当一个请求被代理时,它结束了请求,并且响应现在被管道/发送回客户端。
标签: javascript node.js express http-proxy