0

我目前正在nodejs中开发一个webapp。我在express框架上使用compoundjs。

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

我只想处理不可用的控制器操作。我做不到。

提前致谢。

4

1 回答 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');
    });
于 2014-02-25T11:37:53.600 回答