node.jsweb-applicationsexpressroutescompoundjs

How can i put a 404 page just using the routes.js?


I am currently working on a webapp in nodejs. I am using compoundjs over express framework.

How can i put a 404 page just using the routes.js? There is no proper documentation to do so.

I just want to handle unavailable controller actions. Which I am not able to.

Thanks in advance.


Solution

  • Details are in the Express 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');
        });