我收到以下错误:
错误:发送后无法设置标头 . 在ServerResponse.OutgoingMessage.setHeader(http.js:708:11)的ServerResponse.res.setHeader(/home/rahul/workspace1/iranproud/node_modules/express/node_modules/connect/lib/patch.js:63:22)at at接下来是/(home / rahul / workspace1 / iranproud / node_modules / express / node_modules / connect / lib)/(home / rahul / workspace1 /middleware/session.js:318:9)/ home / rahul / workspace1 / iranproud / / home / rahul / workspace1 / iranproud / /home/rahul/workspace1/iranproud/node_modules/express/node_modules/connect/lib/middleware/session.js:342:9 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在commandHandler上的.nextObject(/home/rahul/workspace1/iranproud/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:678:5)(/ home / rahul / workspace1 / iranproud / node_modules / connect-服务器上的mongo / node_modules / mongodb / lib / mongodb / cursor.js:658:14) . 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时它会自动连接到数据库并且站点正在运行但是在控制台中我收到错误
错误:发送后无法设置标头 .
1 回答
我在想的是,1 - next被调用并且响应被发送到客户端,但是2--在4秒之后你试图发送res.render()..这也被调用了 . 没有看到完整的代码我不知道如何帮助你 .