【问题标题】:Node Express hangs on requestNode Express 应请求挂起
【发布时间】:2016-09-23 07:31:02
【问题描述】:

这是一个棘手的问题;我无法准确指出我的问题出在哪里,但我已经在 mac 和 windows 上重现了。

我有一个与 webpack 捆绑在一起的 Angular 2 站点,它通过节点为我的开发环境使用 express 服务器。我的网站将加载,但在某一时刻,有一个对 json 文件 /docs/package.json 的 http get 请求挂在“待处理”状态。它永远不会通过,最终会失败而没有错误。

有趣的注意事项:

  • 我所有的同事都在同一个提交上工作。

  • Angular 会在调用前立即触发console.log,但它永远不会到达回调。因此,我已经排除了 Angular 作为根本原因,因为在网络面板中查看,请求发生但响应从未到达。

相关代码sn-p为:

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;
}
  • 我已经重新安装了我的依赖项,包括删除了我的 node_modules 目录。
  • 我已将整个存储库炸毁并重新拉出,并重新安装了所有 dep。同样的问题。
  • 我已删除 node 和 nvm,重新安装,核对 repo 并将 repo 重新安装到另一个文件夹中。同样的问题。
  • 我已尝试修改/etc/hosts 以查看别名 localhost 是否会有所帮助。没用。
  • 我已经在节点版本5.8.05.10.06.3.0 上尝试过这个,多个同事都尝试过并成功使用了所有版本。没用。
  • 我已经从终端以及 Intellij 中的内置终端运行了命令。没用。
  • 我没有更改任何代码。我昨天下班时这个项目运行良好。
  • 我已经在 express 的多个不同端口上尝试过。
  • MacOS 在一夜之间更新。不确定这是否会产生影响。
  • 该项目还有一个 python 服务器,python 服务器工作正常,这意味着这不太可能是与端口相关的问题。
  • 此问题在 Chrome、Safari 和 Firefox 中仍然存在。

我怀疑这是 node/express 的问题,因为对于 python 版本,webpack 似乎完全可以捆绑,并且 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/ 开头的请求上错误地触发。

我仍然不确定为什么 globbing 机制会错误地解析它,但我会在弄清楚后回帖...

【问题讨论】:

    标签: angularjs node.js express


    【解决方案1】:

    回答我自己的问题,出于某种原因,节点通配符匹配/docs/ 的所有请求,无论它们是否具有 .html 扩展名,包括 package.json。

    由于我无法弄清楚为什么我的机器上的 globbing 似乎被破坏了,所以如果 if 子句在绕过函数中失败,则返回请求 url:

    proxy: {
        '/docs/**/*.html': {
            secure: false,
            bypass: function (req, res, proxyOptions) {
                if (req.headers.accept.indexOf('html') !== -1) {
                    return '/index.html';
                }
                return req.url;
            }
        }
    }
    

    注意:我喜欢这样。我宁愿正确地使用通配功能......我不知道我刚刚在网站上的其他地方用这个“修复”破坏了什么。如果有人对 globbing 问题有任何想法,我很乐意将不同的答案标记为正确。但在那之前,就是这样。

    【讨论】:

      猜你喜欢
      • 2014-04-30
      • 2014-12-29
      • 2019-02-07
      • 2017-06-23
      • 1970-01-01
      • 2022-11-07
      • 2014-01-14
      • 2020-12-20
      • 2014-11-25
      相关资源
      最近更新 更多