webpack dev server:TypeError:标头内容包含无效字符

时间:2016-11-02 14:53:17

标签: node.js webpack webpack-dev-server http-proxy

我有一个webpack应用程序,我在开发模式下使用webpack dev服务器来运行该应用程序。我也在使用代理将我的请求代理到实时环境。

该应用程序以前使用节点v4.3.2运行,没有任何问题,应用程序运行正常并正确代理。我今天将节点升级到7.0.0版本,因此我可以增加对es6的支持。现在运行应用程序时,我收到了以下所有请求的错误:

webpack: bundle is now VALID.
_http_outgoing.js:360
throw new TypeError('The header content contains invalid characters');
^

TypeError: The header content contains invalid characters
at ServerResponse.setHeader (_http_outgoing.js:360:11)
at /home/jason/dev/frontend-tsm-holiday-results/node_modules/http-      proxy/lib/http-proxy/passes/web-outgoing.js:96:13
at Array.forEach (native)
at Array.writeHeaders (/home/jason/dev/frontend-tsm-holiday-results/node_modules/http-proxy/lib/http-proxy/passes/web-outgoing.js:90:35)
at ClientRequest.<anonymous> (/home/jason/dev/frontend-tsm-holiday-results/node_modules/http-proxy/lib/http-proxy/passes/web-incoming.js:157:20)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:473:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
at TLSSocket.socketOnData (_http_client.js:362:20)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
at TLSWrap.onread (net.js:551:20)

我有一个谷歌,并找到了一些参考这个问题,但我不知道错误来自哪里,为什么?这似乎是http_proxy的一个问题,但我不确定它来自哪个模块。

我对webpack的npm配置是:

"webpack": "^1.13.2",
"webpack-dev-middleware": "^1.6.1",
"webpack-dev-server": "^1.14.1",
"webpack-fail-plugin": "^1.0.5",
"webpack-hot-middleware": "^2.12.2",
"webpack-merge": "^0.14.1"

1 个答案:

答案 0 :(得分:0)

在dev环境中修复此问题的快速方法是编辑实际的node_modules/http-proxy/lib/http-proxy/passes/web-outgoing.js并替换行

res.setHeader(String(key).trim(), header);

类似

      try {
        res.setHeader(String(key).trim(), header);
      } catch (err) {
          console.log(key, header);
      }

正确的解决方法是停止从您正在代理的服务器发送无效字符,或者升级到WebPack 2.x.