我目前正在使用Node.js中的Web应用程序。 我在express框架上使用compoundjs。
我如何仅使用routes.js放置404页面? 没有适当的文档来这样做。
我只想处理不可用的控制器动作。 我不能。
提前致谢。
详细信息在Express 示例中 :
// Error handlers
// Since this is the last non-error-handling
// middleware use()d, we assume 404, as nothing else
// responded.
// $ curl http://localhost:3000/notfound
// $ curl http://localhost:3000/notfound -H "Accept: application/json"
// $ curl http://localhost:3000/notfound -H "Accept: text/plain"
app.use(function(req, res, next){
res.status(404);
// respond with html page
if (req.accepts('html')) {
res.render('404', { url: req.url });
return;
}
// respond with json
if (req.accepts('json')) {
res.send({ error: 'Not found' });
return;
}
// default to plain-text. send()
res.type('txt').send('Not found');
});