express 404 error on defined route code example

Example 1: set 404 handling via express in node

app.use(function(req, res, next){
  res.status(404);

  // respond with html page
  if (req.accepts('html')) {
    res.render('404', { url: req.url });
    return;
  }

  // respond with json
  if (req.accepts('json')) {
    res.send({ error: 'Not found' });
    return;
  }

  // default to plain-text. send()
  res.type('txt').send('Not found');
});

Example 2: node express dynamic route and error handler

app.use(function (req, res, next) {
  res.status(404).send("Sorry can't find that!")
})