Mike Valstar

Web Development, Programming, Random Thoughts

Twitter Status...

Coding with Node.js: Part 2; Error Handling and 404 pages with Express.

Posted on
Code Updated The code below has been updated for express 3.0 here

See Coding with Node.js: Part 1; Getting started with Express. for the beginning of this tutorial series.

The next step for coding a website (for me) is to setup the error and 404 pages. The 404 page to be used to display something useful to the user when something cannot be found. A nice error page can also be helpful in debugging and for informing the general public that an error has occurred on your website.

The 404 page

The 404 page for the purpose of this tutorial will be a simple 404.jade file. Feel free to make this page look however you would like.

App.js

In the App.js file you need to add a handler for not found pages, this is done by registering with express a function that accepts 3 arguments without the usual page identifier:

This is all that is needed and your new 404 page will show in the event of any file or page not being found.

Example

An example of this page can be found by going to /404Error or by typing some gibberish into URL

Displaying Errors

The next item you will want to do is to display any page errors that occur in a meaningful way for testing, and as a way to alert the user that something has gone wrong. Express allows you to set both a production and a development level of error capturing, logging and whatever. To start with this section I used the default error handler as a starting example. See here for the default error handler class for Express, and is provided by Connect

ErrorHandler.js

Based on the error handler provided by connect I built the following:

I have with this error handler I have converted over to using the express render stack for displaying html pages instead of reading files directly and have added the ability to set options for the error handler including the ability to log errors to a file. The example above does have one flaw, that if there is an error with one of the core components it will have trouble rendering and may cause problems. These errors should still be spinout to the console (and optionally the log file) and should be solvable. Additionally a try/catch could be thrown around the render stack with an irrecoverable error displayed; I'll leave this up to the reader if they wish to implement it (I will be doing so at a later date).

App.js

Now to use the new error handler we need to change the app.js file to register the new error handler and to give us a couple example pages.

This will register the error handler for use with both a dev environment and a production one. Also included is 2 examples of triggering an error.

Example

You can check out the the 2 types of errors included in the above by visiting /ErrorExample or /ErrorExample2.

The Code

All code created for this website is available on the github page and this tutorial specifically is available here: https://github.com/mikevalstar/mikevalstar_com/tree/Node_Tutorial_pt2

Next Time

In the next part of this series I will be connecting up the project to a database. Please Stay Tuned...

bp_105
comments powered by Disqus
Mike Valstar © 2012