1
我試圖做一個簡單的代理與快遞js和mikeal /請求庫:
客戶端應用程序調用某些路徑上的expressjs(/ api)和所有請求轉發到遠程機器。
它不工作,我必須失去明顯的東西。nodejs代理與快遞和mikeal /請求庫
expressjs代碼:
app.use('/api', function(req, res) {
var url = config.api + req.url;
var auth = authlib.createTokenHeader(req.user) || {};
var forwarded = { 'X-Forwarded-For': req.connection.remoteAddress };
console.log('Forwarding to api ' + url);
req.pipe(request({
uri:url,
headers: _.merge(authlib.stripSensitiveHeaders(req.headers), auth, forwarded),
})).pipe(res);
});
當發出PUT請求http://localhost:9000/api/account
,咕嚕日誌顯示:
Forwarding to api http://xxx.xxx.xxx/account
PUT /api/account 200 120005ms
Forwarding to api http://xxx.xxx.xxx/account
PUT /api/account 200 120006ms
並在遠程API,Apache日誌顯示,要求被提出:
xx.xx.xx.xx - - [23/Feb/2014:17:44:23 +0000] "PUT /account HTTP/1.1" 400 189 "http://localhost:9000/login" "Mozilla/5.0"
xx.xx.xx.xx - - [23/Feb/2014:17:46:23 +0000] "PUT /account HTTP/1.1" 400 189 "http://localhost:9000/login" "Mozilla/5.0"
最終Chrome發出了收到的空響應:
PUT http://localhost:9000/api/account net::ERR_EMPTY_RESPONSE
問題是爲什麼我得到兩個請求,並且120s超時是從哪裏來的?
我試圖使它simplier,但結果都是一樣的:
app.use('/api', function(req, res) {
req.pipe(request(config.api + req.url)).pipe(res);
});
和它的作品,如果我發佈使用像郵差從Chrome中在同一臺機器上的 http://xxx.xxx.xxx/account端點