0

我收到以下错误:

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.
4

2 回答 2

0

我在想的是,1 - next 被调用并将响应发送给客户端,但是 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


});
于 2015-05-01T14:58:18.363 回答
0

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

于 2019-02-04T18:38:52.537 回答