私はかなり標準的な connect-mongo セットアップを持っています
マングースはこれより前に初期化/接続されています
app.use(express.session({
secret: "sfdgsdgsdfg",
store: new MongoSessionStore({db: mongoose.connection.db})
}));
これはうまくいきます。
ただし、mongodb 接続が突然切断されたと仮定します (以下の例では、mongod をローカルで停止します)。次にルートにアクセスしようとすると、高速アプリもクラッシュします。
エラー: null で [localhost:27017] に接続できませんでした。(/Users/alex/Projects/MyProject/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:540:74) で (events.js:106:17) を発行します。(/Users/alex/Projects/MyProject/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15) ソケットで (events.js:98:17) を発行します。(/Users/alex/Projects/MyProject/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:478:10) Socket.emit (events.js:95:17) で net.js: 440:14 で process._tickCallback (node.js:419:13)
このエラーを処理して (たとえば) /error ルートにリダイレクトする方法はありますか? (明らかに、セッションを必要としないものです!)
編集
だから今、私は別のマングース接続を作成しています。
次に、 を使用しon('error'
てエラーをリッスンします
...これが私が立ち往生している場所です-エラーを再スローしてもエクスプレスエラーハンドラーに渡されないため、プロセスはまだ停止しています...
var sessionDbConnection = mongoose.createConnection(config.sessionDb);
sessionDbConnection.on('error', function(err) {
console.log('oops');
throw err; //instead of re-throwing the error, i think i need to pass it to the error handler below??
})
app.use(express.session({
secret: "sfdgsdgsdfg",
store: new MongoSessionStore({db: sessionDbConnection.db})
}));
app.use(function(err, req, res, next) {
res.render('error', err); //just for testing
});