【问题标题】:Node.js/Express - Render error when page not foundNode.js/Express - 找不到页面时呈现错误
【发布时间】:2011-11-30 07:04:40
【问题描述】:

我在 Node.js 中有以下控制器/路由定义(使用 Express 和 Mongoose)。当用户请求一个不存在的页面时,处理错误的最简单最合适的方法是什么?

  app.get('/page/:pagetitle', function(req, res) {
      Page.findOne({ title: req.params.pagetitle}, function(error, page) {
          res.render('pages/page_show.ejs',
            { locals: {
                title: 'ClrTouch | ' + page.title,
                page:page
            }
          });
      });
  });

它目前破坏了我的应用程序。我相信因为我没有对错误做任何事情,我只是将它传递给视图,就像成功一样?

TypeError: Cannot read property 'title' of null

非常感谢。

【问题讨论】:

    标签: javascript node.js express mongoose


    【解决方案1】:

    Node.JS 的主要问题之一是无法捕获干净的错误。常规的方式通常是对每个回调函数,如果有错误,第一个参数是not null,例如:

    function( error, page ){
       if( error != null ){
           showErrorPage( error, req, res );
           return;
       }
       ...Page exists...
    }
    

    如果有太多回调,事情可能会在一段时间后变得丑陋,我建议使用async 之类的东西,这样如果有一个错误,它就会直接进入错误回调。

    编辑:您也可以使用express error handling

    【讨论】:

    • +1 为您的答案,因为它很有帮助。不应该有undefined 而不是null
    • @SushantGupta 不,Nican 是正确的。它要么为空,要么有错误对象。
    【解决方案2】:

    查看 express error-pages 示例。原则是首先注册您的应用程序路由,然后为所有其他未映射到路由的请求注册一个 catch all 404 处理程序。最后注册一个500的handler,如下:

    // "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
      });
    });
    

    【讨论】:

    • 是的,我觉得我已经完成了 Express 示例。但这无论如何还是很有帮助的。我在视图中创建了自己的 500.ejs 等……现在根据需要进行渲染,但我也可以在指定时添加自己的错误消息。谢谢。
    • 当我在 app.use() 中添加 404 处理时,由于某种原因,它会弄乱我所有到公共文件夹中的东西的路线。就像 /stylesheets/style.css 突然丢失了,因为它通常是从我的 ejs 模板访问的。对此有何建议?
    • 我可以通过简单地将 app.use(static....) 移动到此处给出的示例之前来解决 express app.use() 中间件破坏我的静态路径的问题
    • 理想情况下,您不希望将 500 错误消息的具体原因显示给客户端,只需将其记录并显示 Something went wrong
    • app.router 已弃用
    猜你喜欢
    • 1970-01-01
    • 2013-02-03
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多