【问题标题】:Nodejs + Can't set headers after they are sentNodejs +发送后无法设置标头
【发布时间】:2016-09-01 13:17:22
【问题描述】:

我正在使用节点 js,我也在请求其他一些第三方 coap url。

当我运行此代码时出现错误:发送后无法设置标头。

不知道我犯了什么错误。

财政年度:https://github.com/mcollina/node-coap

代码:

var app = require('express');
var router = app.Router();
var coap = require('coap');
var Q = require('q');
router.get('/test', function(req, res, next) {    
    var result = {
        resp : {},
    };    
    Q(result).then(function(result) {
        var deferred = Q.defer();    
        var coapConnection = {
            host : 'abcd.com',
            pathname : '/get',
            method : 'GET',
            port : '5683',
            confirmable : true
        };
        var req = coap.request(coapConnection);
        req.write(JSON.stringify(result.body));
        req.on('response', function(response) {
            var d = response.payload;
            result.resp.response = JSON.parse(d.toString('utf8'));
            deferred.resolve(result);
            response.pipe(process.stdout);
            response.on('end', function() {
                //process.exit(0);
                res.send(result.resp);
            });
        });
        req.end();
        return deferred.promise;
    }).then(function(result) {
        result.resp.status = 'Success';
        res.send(result.resp.response);
    }).fail(function(result) {
        result.resp.status = 'Error';
        res.send(result.resp);
    });

});

module.exports = router;

错误:

throw new Error('Can\'t set headers after they are sent.');
          ^
Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
    at ServerResponse.res.set.res.header (test/node_modules/express/lib/response.js:524:10)
    at ServerResponse.res.json (test/node_modules/express/lib/response.js:189:36)
    at ServerResponse.res.send (test/node_modules/express/lib/response.js:118:21)
    at IncomingMessage.<anonymous> (test/routes/coap/trigger.js:111:9)
    at IncomingMessage.emit (events.js:129:20)
    at endReadableNT (test/node_modules/coap/node_modules/readable-stream/lib/_stream_readable.js:865:12)
    at afterTickTwo (test/node_modules/coap/node_modules/readable-stream/node_modules/process-nextick-args/index.js:27:10)
    at process._tickCallback (node.js:355:11)

请任何人提供此问题的解决方案。 谢谢。

【问题讨论】:

    标签: javascript node.js coap


    【解决方案1】:

    错误消息告诉您,您在向客户端发送重播后尝试设置标头。在您的情况下,问题应该是 res.send 被调用了 2 次。您在调用响应事件时发送,然后在随后的函数中发送。

    Express 在这种情况下的工作方式与 nodejs 本身有点不同。

    【讨论】:

      【解决方案2】:

      您的问题可能在于多次创建网站。您的错误意味着标题(ergo, page)已经创建,并且您正在尝试重新创建它。

      这里的罪魁祸首是这段代码:

      response.on('end', function() {
          //process.exit(0);
          res.send(result.resp);
      });
      

      与以下冲突:

      .then(function(result) {
          result.resp.status = 'Success';
          res.send(result.resp.response);
      })
      

      因为后者试图推翻最初的res.send()

      【讨论】:

        猜你喜欢
        • 2015-12-20
        • 2016-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-23
        • 2017-12-22
        • 2018-09-02
        • 2023-04-01
        相关资源
        最近更新 更多