how to replace cannot get with 404 error 1

how to replace cannot get with 404 error

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

Here is what the above code is Doing:
1. We’re using the app.use() method to add a middleware function to the stack.
2. The middleware function is checking the request’s Accept header to see if the client is asking for HTML or JSON.
3. If the client is asking for HTML, we’re rendering a 404 page.
4. If the client is asking for JSON, we’re sending a JSON response.
5. If the client is asking for something else, we’re sending a plain-text response.

Similar Posts