Node.js 在express中,当满足条件时,如何路由到自定义错误模板?

Node.js 在express中,当满足条件时,如何路由到自定义错误模板?,node.js,express,error-handling,pug,Node.js,Express,Error Handling,Pug,在select语句中,如果URL中的参数不正确,我希望将用户发送到自定义错误模板 我已尝试设置渲染,但传递的参数无效 #routes.js router.get('/select-id/', function(req, res) { var id = req.query.id; if(id && id.match(/^[0-9]+$/) != null){ mc('SELECT * FROM tasks where ID = ' + id, fun

在select语句中,如果URL中的参数不正确,我希望将用户发送到自定义错误模板

我已尝试设置渲染,但传递的参数无效

#routes.js
router.get('/select-id/', function(req, res) {
    var id = req.query.id;
    if(id && id.match(/^[0-9]+$/) != null){
        mc('SELECT * FROM tasks where ID = ' + id, function (error, results, fields) {
         if (error) throw error;
          res.render('pug-select-all', {
            title: 'Results',
            data: results
          });
        });
    }
    else {
        res.render('error', {status: 500, stack: 'error'});
    }
});

我尝试过不同的方法,但我要么总是在routes.js或pug模板上出错。如果我像上面的代码一样保留它,我会得到错误的
无法读取未定义的属性“status”。帕格查看文档时,您应该将响应对象发送为:

res.render('error', { "error": {status: 500, stack: 'error' } });
在您的
pug
模板中,作为一级成员访问它,如:

#error.pug
extends layout

block content
  h1= message
  h2= status
  pre #{stack}

这就是我的工作方式

res.status(404).render('errorPage', {
  title: '404 - page nt found!'
});
为了满足你的需要

res.status(500).render('error', {
  status: 500, 
  stack: 'error'
});
res.status(500).render('error', {
  status: 500, 
  stack: 'error'
});