express统一输出404页面

不玩不知道,一玩吓一跳,还真是,nodejs全局404怎么搞?

直接,res.render("404.html")有可能会报错:Node.js : Cannot find module html 那怎么解决呢?

答案:

如果你使用的是jade模板,则可以在views文件夹下面新增一个404.jade文件,里面引入一下你写好的404.html就可以了。

404.jade如下,就一行代码

include ../public/404.html 

404.html在public下面,随便发挥吧,html,随便写

就是普通的html

app.js

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  res.render('404');
  next(err); 
});

demo: http://ae6623.cn/404
http://stackoverflow.com/questions/4529586/render-basic-html-view-in-node-js-express

原文地址:https://www.cnblogs.com/ae6623/p/5573044.html