现在访问一个不存在的地址,如:http://localhost:3000/haha 页面会显示:
Cannot GET /haha
我们来自定义 404 页面。修改 routes/index.js,在:
routes/index.js
app.use('/comments', require('./comments'))
下添加如下代码:
// 404 pageapp.use(function (req, res) {if (!res.headersSent) {res.status(404).render('404')}})
新建 views/404.ejs,添加如下代码:
views/404.ejs
<!DOCTYPE html><html><head><meta charset="utf-8"><title><%= blog.title %></title><script type="text/javascript" src="http://www.qq.com/404/search_children.js" charset="utf-8"></script></head><body></body></html>
这里我们只为了演示 express 中处理 404 的情况,用了腾讯公益的 404 页面,刷新一下页面看下效果吧。
