node.js - Can't set headers after they are sent. Nodejs Express -


i have yet "can't set headers after sent." problem. i've created post router receives large object of urls, need processing.

the processing consists of crawling of urls object contains. i've set bottleneck, maximum of 10 concurrent crawls , wait time of 800 milliseconds.

either way, possible data arrive, , log it, when i'm calling function, suppose process data, application throws exception.

post router

router.post('/crawledurls', function (req, res, err) {     crawler = new crawlerclass();     var urlarray = json.parse(req.body);     crawler.crawlarrayurls(urlarray);     res.send({message: "array sent successfully!"}); }); 

crawler.crawlarrayurls

crawlerclass.prototype.crawlarrayurls = function (arrayobject) { var itemsprocessed = 0; var emptyurlarray = []; var newarray = []; for(keys in arrayobject){     newarray.push(arrayobject[keys]); } var limiter = new bottleneck(10, 800); newarray.foreach(function (listitem, indexarray, err) {     limiter.submit(err, function () {         if(err) throw err;         request({             encoding: null,             method: "get",             headers: {                 'user-agent': 'request'             },             uri: listitem         }, function (error, response, body, err) {             if(err) throw err;             $ = cheerio.load(body);             if($('div.alert.alert-danger').html() == undefined) {                 console.log(listitem);                 emptyurlarray.push(listitem);             } else {                 console.log("else");                 //not done.             }         });     }); }); 

};

app.js

var express = require('express'); var session = require('express-session'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); //var cookieparser = require('cookie-parser'); var bodyparser = require('body-parser'); var scheduleobject = require('./utilitylogic/scheduleobject'); var schedulebidding = require("./googleapilogic/schedulebidding"); var dbmodules = require("./dblogic/dbmodules"); var routes = require('./routes/index');  var app = express();  // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); app.enable('trust proxy');  app.use(logger('dev')); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({ extended: false }));  app.use(session({     secret: 'secret',     resave: false,     saveuninitialized: true,     cookie: {         httponly: true,         secure: true     } })); app.use(express.static(path.join(__dirname, 'public')));  app.use('/', routes);  // catch 404 , forward error handler app.use(function(req, res, next) {   var err = new error('not found');   err.status = 404;   next(err); });  // error handlers  // development error handler // print stacktrace if (app.get('env') === 'development') {   app.use(function(err, req, res, next) {     res.status(err.status || 500);     res.render('error', {       message: err.message,       error: err     });       throw err;   }); }  // production error handler // no stacktraces leaked user app.use(function(err, req, res, next) {   res.status(err.status || 500);   res.render('error', {     message: err.message,     error: {}   }); });  var scheduler = new scheduleobject(); var dbobject = new dbmodules("./somepath"); var schedulebidobject = new schedulebidding("somekey");  //test connection db dbobject.testconnectiontodb();  scheduler.schedulefunction('59 59 23 * * *',function (){     schedulebidobject.getbiddingheaders('/somepath/'); });  scheduler.schedulefunction('01  e 10 00 * * *',function (){     schedulebidobject.getbiddingheaders('/somepath/'); });  module.exports = app; 

exception thrown

error: can't set headers after sent. www-2     @ serverresponse.outgoingmessage.setheader (http.js:691:11) www-2     @ serverresponse.header (/somepath/node_modules/express/lib/response.js:719:10) www-2     @ serverresponse.send (/somepath/node_modules/express/lib/response.js:164:12) www-2     @ done (/somepath/node_modules/express/lib/response.js:956:10) www-2     @ object.exports.renderfile (/somepath/node_modules/jade/lib/index.js:374:12) www-2     @ view.exports.__express [as engine] (/somepath/node_modules/jade/lib/index.js:417:11) www-2     @ view.render (/somepath/node_modules/express/lib/view.js:126:8) www-2     @ tryrender (/somepath/node_modules/express/lib/application.js:639:10) www-2     @ eventemitter.render (/somepath/node_modules/express/lib/application.js:591:3) www-2     @ serverresponse.render (/somepath/node_modules/express/lib/response.js:960:7) 

the problem was returning the exception through jade template meanwhile tried return json response. following edit solved problem:

before:

app.use(function(err, req, res, next) {   res.status(err.status || 500);   res.render('error', {     message: err.message,     error: {}   }); }); 

after:

app.use(function(err, req, res, next) {     if(req.body){         throw err;     } else {         res.status(err.status || 500);         res.render('error', {             message: err.message,             error: {}         });     } }); 

Comments