How can I get Express.js to 404 only on missing routes?

前端 未结 4 527
生来不讨喜
生来不讨喜 2020-12-05 09:20

At the moment I have the following which sits below all my other routes:

app.get(\'*\', function(req, res){
  console.log(\'404ing\');
  res.render(\'404\');         


        
4条回答
  •  失恋的感觉
    2020-12-05 10:10

    I wanted a catch all that would render my 404 page only on missing routes and found it here in the error handling docs https://expressjs.com/en/guide/error-handling.html

    app.use(function (err, req, res, next) {
      console.error(err.stack)
      res.status(404).render('404.ejs')
    })
    

    This worked for me.

提交回复
热议问题