我收到以下错误:
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.