Answers for "express 404"

1

express 404

// Dopo tutte le altre route

app.get('*', function(req, res, next) {
    var err = new Error();
    err.status = 404;
    next(err);
});

app.use(function(err, req, res, next) {
    if (err.status === 404) {
        var data = {
            title: '404 Not Found',
            content: 'Oops, page not found!';
        };
        res.render('pages/404', data);
    } else {
        return next();
    }
});
Posted by: Guest on March-24-2021
1

express 404

app.use(function(req, res, next){
  res.status(404);

  // respond with html page
  if (req.accepts('html')) {
    res.sendFile('index.html');
    return;
  }

  // respond with json
  if (req.accepts('json')) {
    res.send({
      status: 404,
      error: 'Not found'
    });
    return;
  }

  // default to plain-text. send()
  res.type('txt').send('404 - Not found');
});
Posted by: Guest on July-06-2020
0

set 404 handling via express in node

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');
});
Posted by: Guest on May-25-2020
0

node express dynamic route and error handler

app.use(function (req, res, next) {
  res.status(404).send("Sorry can't find that!")
})
Posted by: Guest on January-28-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language