私はエラー処理をexpressで実行しようとしていますが、 "error !!!"という応答は表示されません。私はコンソールに「何らかの例外」があり、プロセスが強制終了されることを期待しています。これはエラーハンドリングがどのようにセットアップされているのでしょうか?それでエラーを捕まえる別の方法があるのでしょうか?あなたのコードが働いていなかったExpress jsエラー処理
1):
// Require Dependencies
var express = require('express');
var app = express();
// Middleware
app.use(app.router); // you need this line so the .get etc. routes are run and if an error within, then the error is parsed to the next middleware (your error reporter)
app.use(function(err, req, res, next) {
if(!err) return next(); // you also need this line
console.log("error!!!");
res.send("error!!!");
});
// Routes
app.get('/', function(request, response) {
throw "some exception";
response.send('Hello World!');
});
// Listen
app.listen(5000, function() {
console.log("Listening on 5000");
});
将来的には、3.x => 4.xの移行プロセスでは、app.routerは廃止予定です。 https://github.com/strongloop/express/wiki/Migrating-from-3.x-to-4.xを参照してください。 –