我試圖通過express.js從我的角客戶端應用程序代理請求。這是中間件向遠程服務器發出請求
app.use('/v1', function(req, res) {
var url = process.env.API_URL + req.url;
console.info('Proxing to: ', url);
var r = null;
switch(req.method) {
case 'POST':
r = request.post({uri: url, json: req.body });
break;
case 'PUT':
r = request.put({uri: url, json: req.body });
break;
default:
r = request(url);
}
console.log(r);
return req.pipe(r).pipe(res);
});
快遞服務器正在運行http://localhost:3030。中間件適用於http://localhost:3030/v1的GET請求,但它不適用於POST或PUT請求。 當我發出PUT或POST請求時服務器被終止。這是錯誤
stream.js:74
throw er; // Unhandled stream error in pipe.
^
Error: write after end
at ClientRequest.OutgoingMessage.write (_http_outgoing.js:426:15)
at Request.write (/Users/user/Projects/maxmc/node_modules/request/request.js:1514:27)
at end (/Users/user/Projects/maxmc/node_modules/request/request.js:552:18)
at Immediate._onImmediate (/Users/user/Projects/maxmc/node_modules/request/request.js:581:7)
at tryOnImmediate (timers.js:543:15)
at processImmediate [as _immediateCallback] (timers.js:523:5)
error Command failed with exit code 1.
我不能確定問題是什麼。
看來你越來越成功 –