Question

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.

Was it helpful?

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');
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top