我已经使用 node、express 和 express-http-proxy 设置了一个本地代理服务器,它将请求重定向到远程后端,该后端只能通过 VPN 使用。这在建立 VPN 连接时效果很好。但是,当没有 VPN 连接时,我收到此错误:
_http_outgoing.js:346
throw new Error('不能在发送后设置标题。');
^
错误:发送后无法设置标头。
在 ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:346:11)
在/path/to/my/project/proxy/node_modules/express/lib/application.js:152:9
接下来(/path/to/my/project/proxy/node_modules/express/lib/router/index.js:150:14)
在 SendStream.error (/path/to/my/project/proxy/node_modules/serve-static/index.js:74:37)
在emitOne (events.js:77:13)
在 SendStream.emit (events.js:169:7)
在 SendStream.error (/path/to/my/project/proxy/node_modules/serve-static/node_modules/send/lib/send.js:147:51)
在 SendStream.onStatError (/path/to/my/project/proxy/node_modules/serve-static/node_modules/send/lib/send.js:248:48)
在/path/to/my/project/proxy/node_modules/serve-static/node_modules/send/lib/send.js:320:26
在 FSReqWrap.oncomplete (fs.js:82:15)
我的代理设置是:
app.use('/the/api', proxy('the.domain.behind.vpn:8026', {
超时:4000,
转发路径:函数(请求,资源){
console.log(nodeUrl.parse(req.url).path);
返回 "/the/api" + nodeUrl.parse(req.url).path;
},
拦截:函数(rsp,数据,req,res,回调){
data = data.toString("utf8").replace(/http\:\/\/the\.domain\.behind\.vpn\:8026/g, "http://localhost:61000");
回调(空,数据);
},
装饰请求:函数(代理请求,原始请求){
if(proxyReq.method === "HEAD"){
proxyReq.method = "GET";
}
返回代理请求;
}
}));
导致此错误的原因是什么,我该如何捕捉?