[英]Node Express hangs on request
這是一個棘手的問題。 我無法確切指出問題的出處,但我已經在Mac和Windows上進行了轉載。
我有一個與webpack捆綁在一起的Angular 2站點,該站點通過我的開發環境的節點使用Express服務器。 我的網站將加載,但有一次,有一個http get請求,請求將json文件/docs/package.json
掛在'pending'上。 它永遠不會通過,最終將失敗且沒有錯誤。
有趣的事情:
我所有的同事都在同一提交上工作該項目。
Angular會在進行調用之前立即觸發console.log
,但它永遠不會到達回調。 因此,我排除了Angular的根本原因,因為在網絡面板中查找到了請求,但是響應從未到來。
相關代碼段為:
init() {
this.onReadyPromise = new Promise((resolve, reject)=> {
console.log('This log will fire');
this.http.get(`${this.constants['BASE_RESOURCES_PATH']}/${this.constants['JSON_FILE_NAME']}`)
.map(res => res.json())
.subscribe(
console.log('This log will NOT fire');
response => {
this.parseDocsJson(response)
.then(()=> resolve(this.docs));
},
err => reject(err)
);
});
return this.onReadyPromise;
}
/etc/hosts
來查看別名localhost是否會有所幫助。 沒用 5.8.0
, 5.10.0
和6.3.0
,多個同事曾嘗試與成功的所有版本。 沒用 我懷疑這是node / express的問題,因為webpack對於python版本似乎完全可以捆綁,並且Angular也可以通過python服務器正常工作。
我希望我需要為可能會提供幫助的任何人提供有關服務器配置的更多信息,但這是我目前所擁有的:
server.dev.js:
const express = require('express');
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const config = require('./webpack.dev');
const helpers = require('./helpers');
const HOST = '0.0.0.0';
const PORT = 3000;
/**
* Webpack Development Server configuration
* Description: The webpack-dev-server is a little node.js Express server.
* The server emits information about the compilation state to the client,
* which reacts to those events.
*
* See: https://webpack.github.io/docs/webpack-dev-server.html
*/
const webpackDevServerOptions = {
historyApiFallback: true,
watchOptions: {
aggregateTimeout: 300,
poll: 1000
},
stats: {
colors: true,
errorDetails: true,
cached: true,
chunks: false
},
contentBase: helpers.root('src'),
outputPath: helpers.root('dist'),
proxy: {
'/docs/**/*.html': {
secure: false,
bypass: function (req, res, proxyOptions) {
if (req.headers.accept.indexOf('html') !== -1) {
return '/index.html';
}
}
}
}
};
var app = new WebpackDevServer(webpack(config), webpackDevServerOptions);
app.use('/docs/', express.static(helpers.root('../docs/')));
app.listen(PORT, HOST, function (err) {
if (err) {
throw err;
}
console.info(`Listening at http://${HOST}:${PORT}`);
});
更新資料
webpackDevServerOptions
proxy
引起了該問題。 由於某種原因,每個請求都會觸發肯定請求。 如果我將console.log
放到繞過函數中,則會在以/docs/
開頭的所有請求上錯誤觸發。
我仍然不確定為什么全局機制會錯誤地解析它,但是一旦我弄清楚了,我會回發...
為了回答我自己的問題,出於某種原因,節點遍歷匹配了/docs/
所有請求,無論它們是否具有.html擴展名(包括package.json)。
由於我無法弄清楚為何在我的計算機上出現globbing的問題,所以臨時解決方法是如果if
子句在bypass函數中失敗,則返回請求URL:
proxy: {
'/docs/**/*.html': {
secure: false,
bypass: function (req, res, proxyOptions) {
if (req.headers.accept.indexOf('html') !== -1) {
return '/index.html';
}
return req.url;
}
}
}
注意:我不喜歡這樣。 我寧願具有正確的globing功能...我也不知道我剛剛用此“修復”在網站上的其他地方破壞了什么。 如果有人對問題有任何想法,我很樂意將其他答案標記為正確。 但是直到那時,僅此而已。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.