如何捕获nodejs连接静态404错误?

its*_*ony 1 error-handling static connect node.js http-status-code-404

我使用nodeJS和senchalab的连接中间件(不是表达!!).我需要捕获静态错误(如404).我怎么得到这些?我试图检查源,但我找不到将错误处理程序传递给connect.static的方法.

这里是我(必须)使用的一般结构的要点:

https://gist.github.com/2397415

pro*_*ich 5

根据您的路线:

router.routes['/'] = 'my page';
router.routes['/404'] = '404 test';
Run Code Online (Sandbox Code Playgroud)

我想你的意思是:

connect()
  .use(connect.static(__dirname + '/public'))
  .use(function(req, res, next){
    switch (req.url) {
      case '/404':
        var body = '404 test';
        res.statusCode = 404;
        res.setHeader('Content-Length', body.length);
        res.end(body);
        break;
      default:
        var body = 'my page';
        res.setHeader('Content-Length', body.length);
        res.end(body);
    }
  })
  .listen(3001);
Run Code Online (Sandbox Code Playgroud)

另外我想在旧的连接版本1.x中添加你可以使用'connect.router',但是在2.x版本中它被移除并移动到express.如果您需要有用的路由系统,请使用express:

Connect从未打算直接使用,它的存在是为了支持像Express这样的框架