JavaScript Handling 404, 500 and Exceptions in Node.js and Express handling exceptions node.js,handle all exceptions node,handling uncaught

I have a node.js + Express + express-handlebars app. I want to redirect the users to 404 page when they go to a page that does not exists and redirect them to a 500 when there is an internal server error or an exception(without stopping the server). In my app.js I have written the middle ware at the end to perform these tasks.

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

//Handle 404
app.use(function(err, req, res, next){
    res.sendStatus(404);
    res.render('404');
    return;
});

//Handle 500
app.use(function(err, req, res, next){
    res.sendStatus(500);
    res.render('500');
});

//send the user to 500 page without shutting down the server
process.on('uncaughtException', function (err) {
  console.log('-------------------------- Caught exception: ' + err);
    app.use(function(err, req, res, next){
        res.render('500');
    });
});

However only the code for 404 works. So if I try to go to an url

localhost:8000/fakepage

it successfully redirects me to my 404 page. The 505 does not work. And for the exception handling, the server does keep running but, it does not redirect me to the 500 error page after the console.log

I am confused by so many solutions online where people seem to implement different techniques for this.

Here are some of the resources I looked at

http://www.hacksparrow.com/express-js-custom-error-pages-404-and-500.html

Correct way to handle 404 and 500 errors in express

How to redirect 404 errors to a page in ExpressJS?

https://github.com/expressjs/express/blob/master/examples/error-pages/index.js

Answer:1

I am making a form with some fields that a user has to fill out. Once he/she fills it out, hits save, the form disappears and a new div appears with what the user filled out. Unfortunately, I cannot ...

I am making a form with some fields that a user has to fill out. Once he/she fills it out, hits save, the form disappears and a new div appears with what the user filled out. Unfortunately, I cannot ...

I tried to return data after update in firebase but it is displaying as undefined test().then((res) => { alert(res); }); test() { var fredNameRef = new Firebase('https://...

I tried to return data after update in firebase but it is displaying as undefined test().then((res) => { alert(res); }); test() { var fredNameRef = new Firebase('https://...

  1. return success data ajax jquery
  2. return success data
  3. return success data ajax
  4. javascript return success data
  5. ajax success return data json
  6. jquery success return data
  7. datatable ajax success return data
  8. ajax post success return data

In the past, I typically added my dialog as HTML as shown on the first example. I never liked having to maintain both the HTML page and the JavaScript page, and am thinking of using either the second ...

In the past, I typically added my dialog as HTML as shown on the first example. I never liked having to maintain both the HTML page and the JavaScript page, and am thinking of using either the second ...

I have a navbar in which I can't really see the .active color as I am using a 3rd party theme that I really like, but however I have one problem with the theme. I can't properly see which of my navbar ...

I have a navbar in which I can't really see the .active color as I am using a 3rd party theme that I really like, but however I have one problem with the theme. I can't properly see which of my navbar ...

  1. bootstrap change navbar color
  2. bootstrap change navbar text color
  3. bootstrap change navbar height
  4. bootstrap change navbar background color
  5. bootstrap change navbar toggler color
  6. bootstrap change navbar color on scroll
  7. bootstrap change navbar font family
  8. bootstrap change navbar width
  9. bootstrap change navbar brand color
  10. bootstrap change navbar dropdown color
  11. bootstrap change navbar collapse breakpoint
  12. bootstrap change navbar breakpoint
  13. bootstrap change navbar text size
  14. bootstrap change navbar item color
  15. bootstrap change navbar font color
  16. bootstrap change navbar active color
  17. bootstrap change navbar font
  18. bootstrap change navbar on scroll
  19. bootstrap change navbar collapse color