0

I am using node js, also i am requesting the some other third party coap url.

When I run this code getting the Error: Can't set headers after they are sent.

Don;t know what mistake I done.

FYR: https://github.com/mcollina/node-coap

Code:

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;

ERROR:

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)

Please anyone give the solution for this issue. Thanks.

RSKMR
  • 1,812
  • 5
  • 32
  • 73

2 Answers2

1

The errror message tells you that you try to set a header after you allready send a replay to client. In your case the problem should be that res.send is called 2 times. You send when resposnse event is called and in then function afterward.

Express does work in this case a little bit different to nodejs it self.

CordlessWool
  • 1,388
  • 4
  • 17
  • 35
0

Your problem probably lies with creating the site multiple times. Your error implies that the header (ergo, page) is already created, and you're trying to re-create it.

The culprit here would be this code:

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

Which conflicts with:

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

Because the latter tries to overrule the initial res.send()

roberrrt-s
  • 7,914
  • 2
  • 46
  • 57