【问题标题】:Node: Cannot set headers after they are sent to the client节点:将标头发送到客户端后无法设置标头
【发布时间】:2020-07-02 07:57:16
【问题描述】:

我对以下错误做了一些研究:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

我理解这个想法以及导致它的原因,但我不确定如何为我的控制器代码修复它:

exports.isServerOnline = (req, res, next) => {
    const sock = new net.Socket();
    sock.setTimeout(2500);
    sock.on('connect', function () {
        sock.destroy();
        return res.json({
            "server": "online"
        });
    }).on('error', function (e) {
        return res.json({
            "server": "offline"
        });
    }).on('timeout', function (e) {
        return res.json({
            "server": "offline"
        });
    }).connect(LS_PORT, LS_HOST);
}

我正在使用套接字来检查远程主机是启动还是关闭。这是我的客户端代码(Vue.js):

created() {
    this.getServerStatus();
  },

  methods: {
    getServerStatus() {
      axios
        .get(`${process.env.VUE_APP_BACKEND_URL}/server/status`)
        .then(response => {
          if (response.data.server === "online") {
            // no interesting code here
          } else {
            // no interesting code here
          }
          /* eslint-disable no-console */
        })
        .catch(error => {
          console.log(error);
        });
    }
  }

完整的错误堆栈跟踪:

2020-07-02T07:40:47.743193+00:00 app[web.1]: _http_outgoing.js:518
2020-07-02T07:40:47.743201+00:00 app[web.1]: throw new ERR_HTTP_HEADERS_SENT('set');
2020-07-02T07:40:47.743202+00:00 app[web.1]: ^
2020-07-02T07:40:47.743202+00:00 app[web.1]:
2020-07-02T07:40:47.743204+00:00 app[web.1]: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
2020-07-02T07:40:47.743205+00:00 app[web.1]: at ServerResponse.setHeader (_http_outgoing.js:518:11)
2020-07-02T07:40:47.743206+00:00 app[web.1]: at ServerResponse.header (/app/node_modules/express/lib/response.js:771:10)
2020-07-02T07:40:47.743206+00:00 app[web.1]: at ServerResponse.json (/app/node_modules/express/lib/response.js:264:10)
2020-07-02T07:40:47.743206+00:00 app[web.1]: at Socket.<anonymous> (/app/src/controllers/status.js:15:20)
2020-07-02T07:40:47.743207+00:00 app[web.1]: at Socket.emit (events.js:315:20)
2020-07-02T07:40:47.743207+00:00 app[web.1]: at emitErrorNT (internal/streams/destroy.js:92:8)
2020-07-02T07:40:47.743208+00:00 app[web.1]: at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
2020-07-02T07:40:47.743208+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:84:21) {
2020-07-02T07:40:47.743209+00:00 app[web.1]: code: 'ERR_HTTP_HEADERS_SENT'
2020-07-02T07:40:47.743209+00:00 app[web.1]: }

如果有人能指出我正确的方向,我将不胜感激。

【问题讨论】:

    标签: node.js express vue.js


    【解决方案1】:

    我不认为这种方法是正确的伙伴。您正在尝试在 http 端点内创建一个套接字,但这是行不通的。这是我发现的使用 NodeJS Socket 演示的要点。

    https://gist.github.com/tedmiston/5935757

    我会推荐使用 Socket.io 之类的东西。他们有大量的文档可以起步。

    【讨论】:

    • 您好,感谢您的留言。我已经设法让它工作了。显然,我所要做的就是添加 sock.destroy();根据文档,在超时回调中。对于“错误”,这是自动调用的。
    猜你喜欢
    • 1970-01-01
    • 2021-06-18
    • 2022-01-14
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多