在app.js,我有如何在express.js中引发404错误?
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
,所以如果我要求一些不存在的URL像http://localhost/notfound
,上面的代码将被执行。
在存在的URL像http://localhost/posts/:postId
,我想抛出404错误时访问一些不存在postId或删除postId。
Posts.findOne({_id: req.params.id, deleted: false}).exec()
.then(function(post) {
if(!post) {
// How to throw a 404 error, so code can jump to above 404 catch?
}
这是在一个页面请求或页面请求中的承诺内调用Posts.findOne'吗? –