0

I am getting the following error:

Error: Can't set headers after they are sent. 
       at ServerResponse.OutgoingMessage.setHeader (http.js:708:11) 
       at ServerResponse.res.setHeader (/home/rahul/workspace1/iranproud/node_modules/express/node_modules/connect/lib/patch.js:63:22) 
       at next (/home/rahul/workspace1/iranproud/node_modules/express/node_modules/connect/lib/proto.js:156:13) 
       at next (/home/rahul/workspace1/iranproud/node_modules/express/node_modules/connect/lib/middleware/session.js:318:9) 
       at /home/rahul/workspace1/iranproud/node_modules/express/node_modules/connect/lib/middleware/session.js:342:9 
       at /home/rahul/workspace1/iranproud/node_modules/connect-mongo/lib/connect-mongo.js:220:17 
       at /home/rahul/workspace1/iranproud/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/collection.js:953:5 
       at Cursor.nextObject (/home/rahul/workspace1/iranproud/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:678:5) 
       at commandHandler (/home/rahul/workspace1/iranproud/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:658:14) 
       at Server.Base._callHandler (/home/rahul/workspace1/iranproud/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/base.js:382:41)

My configuration is as follows:

I am using connect-mongo as session store, and auto reconnect is true, i.e .

app.use(express.session({

 store: new MongoStore({
        url: 'mongodb://192.168.1.104:27017/db_ip',
        auto_reconnect:true

    }, function(e) {
        //code
    }),
    cookie: {
        expires: new Date(Date.now() + 7200000)
    },    
    secret: '*********'
}));

I am testing this code by manually stopping mongod service and requesting from URL, it is in waiting state (browser continuously running until it gets connected to database), so I have kept the following code for timeout of request (after 4 second it will be redirected to under maintenance page).

app.use(function(req, res, next){

    res.setTimeout(4000, function(){
        console.log('Request has timed out.');

        var fullURL = req.protocol + "://" + req.get('host') + req.url;
        var client_ip = req.connection.remoteAddress;
        console.log("\nRequested Url is : " + fullURL + " :: From IP Address : " + client_ip + " : on date : " + new Date()); 
         res.render('undermaintenance');
        });

   next();

});

but when I restart mongod it will automatically connected to database and site is working, but in the console I am getting this error:

Error: Can't set headers after they are sent.
karel
  • 5,489
  • 46
  • 45
  • 50

2 Answers2

0

What i am thinking is , 1 -- next is called and response is sent to client, but 2-- after 4 seconds you are trying to send res.render().. This is also getting called. Without seeing the full code i am not sure how to assist you.

app.use(function(req, res, next){



    res.setTimeout(4000, function(){
        console.log('Request has timed out.');

        var fullURL = req.protocol + "://" + req.get('host') + req.url;
        var client_ip = req.connection.remoteAddress;
        console.log("\nRequested Url is : " + fullURL + " :: From IP Address : " + client_ip + " : on date : " + new Date()); 
         res.render('undermaintenance'); // ---2

        });

   next(); ---> 1


});
Sathish
  • 2,056
  • 3
  • 26
  • 40
0

Whenever you see a message about the fact that you can't send headers twice, it means that you are trying to send a response on a particular http request more than once. Some part of your code (or dependencies) already sent a response. Check all your custom middleware and make sure the callback in res.setTimeout() is not executed every time, even after the very timeout.

Vladyslav Usenko
  • 2,296
  • 8
  • 18