예 : error-pages 예를 확인하십시오. 원칙적으로 앱 경로를 등록한 다음 경로에 매핑되지 않은 다른 모든 요청에 대해 모든 404 핸들러를 등록합니다. 마지막으로 500 핸들러가 다음과 같이 등록됩니다.
// "app.router" positions our routes
// specifically above the middleware
// assigned below
app.use(app.router);
// Since this is the last non-error-handling
// middleware use()d, we assume 404, as nothing else
// responded.
app.use(function(req, res, next){
// the status option, or res.statusCode = 404
// are equivalent, however with the option we
// get the "status" local available as well
res.render('404', { status: 404, url: req.url });
});
// error-handling middleware, take the same form
// as regular middleware, however they require an
// arity of 4, aka the signature (err, req, res, next).
// when connect has an error, it will invoke ONLY error-handling
// middleware.
// If we were to next() here any remaining non-error-handling
// middleware would then be executed, or if we next(err) to
// continue passing the error, only error-handling middleware
// would remain being executed, however here
// we simply respond with an error page.
app.use(function(err, req, res, next){
// we may use properties of the error object
// here and next(err) appropriately, or if
// we possibly recovered from the error, simply next().
res.render('500', {
status: err.status || 500
, error: err
});
});
예, 익스프레스 예제를 통과 한 것처럼 느껴집니다. 그러나 이것은 어쨌든 매우 도움이되었습니다. 나는 내 자신의 500.ejs 등을 만들었습니다 ... 뷰에서 그리고 지금은 필요에 따라 렌더링하지만, 내 자신의 오류 메시지도 지정할 수 있습니다. 감사. – tuddy
404 처리로 app.use()를 추가하면 어떤 이유에서든 공용 폴더에있는 모든 경로가 망가집니다. /stylesheets/style.css처럼 내 ejs 템플릿에서 정상적으로 액세스하므로 갑자기 잃어 버리게됩니다. 이것에 대한 제안? – netpoetica
여기서 주어진 예제 앞에 app.use (정적 ....)를 이동하여 내 정적 경로를 깨뜨리는 express app.use() 미들웨어로 문제를 해결할 수있었습니다. – netpoetica