我如何仅使用routes.js放置404页面?

问题描述 投票:0回答:1

我目前正在使用Node.js中的Web应用程序。 我在express框架上使用compoundjs。

我如何仅使用routes.js放置404页面? 没有适当的文档来这样做。

我只想处理不可用的控制器动作。 我不能。

提前致谢。

node.js web-applications express routes compoundjs
1个回答
1
投票

详细信息在Express 示例中

// Error handlers

// Since this is the last non-error-handling
// middleware use()d, we assume 404, as nothing else
// responded.

// $ curl http://localhost:3000/notfound
// $ curl http://localhost:3000/notfound -H "Accept: application/json"
// $ curl http://localhost:3000/notfound -H "Accept: text/plain"

    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');
    });
© www.soinside.com 2019 - 2024. All rights reserved.