错误:发送后无法设置标头。当自动重新连接到mongodb时

时间:2015-05-01 06:09:26

标签: node.js mongodb http-headers response.redirect connect-mongo

我收到以下错误:

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)

我的配置如下:

我使用connect-mongo作为会话存储,并且自动重新连接是真的,即

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: '*********'
}));

我通过手动停止mongod服务并从URL请求来测试此代码,它处于等待状态(浏览器持续运行,直到它连接到数据库),所以我保留了以下代码用于请求超时(4秒后)它将被重定向到维护页面下。)

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();

});

但是当我重新启动mongod时它会自动连接到数据库并且网站正常工作,但在控制台中我收到此错误:

Error: Can't set headers after they are sent.

2 个答案:

答案 0 :(得分:0)

我在想的是,1 - 接下来被调用并且响应被发送到客户端,但是2--在4秒之后你试图发送res.render()..这也被调用。没有看到完整的代码我不知道如何帮助你。

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


});

答案 1 :(得分:0)

每当您看到有关无法两次发送标头的消息时,这意味着您尝试多次针对特定的http请求发送响应。您的代码的某些部分(或依赖项)已经发送了响应。 检查所有自定义中间件,并确保res.setTimeout()中的回调即使在超时后也不会每次都执行。