📜  如何替换无法获得 404 错误 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:29.593000             🧑  作者: Mango

代码示例1
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');
});