2014-02-25 32 views
0

我目前正在研究nodejs中的webapp。我正在使用複合js表達框架。我怎樣才能把404頁面只使用routes.js?

我怎樣才能把404頁只使用routes.js?沒有適當的文件來這樣做。

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

在此先感謝。

回答

1

細節在快遞examples

// 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'); 
    });