Javatpoint Logo
Javatpoint Logo

Adding error handling in server.js in MEAN Stack

In our previous section, we learned how to add node and express backend. We got the basic node and the express app set up. Now, we will improve our server.js file by simply adding some error handling and better output. We will add the following lines of code in our server.js file:

1) We will create a normalizePort function. This function will be a bit of a more elaborate function, which makes sure that when we try to set up a port and when we receive it through an environmental variable, it is a valid number. This function will be coded as:

In the above code, we parse our port into an integer. If the port number is illegal, it returns the val, i.e., our original port, which we passed to the function. If the port is greater or equal to the zero, it returns the port, which we have parsed.

Adding error handling in server.js in MEAN Stack

2) After that, we will create onError function. This function will check which type of error occurred, log something different, and exit gracefully from our nodejs server. This function will be coded in the following way:

The above code checks for the system call and shows the port's error like elevated privileges, already in use, etc.

Adding error handling in server.js in MEAN Stack

3) We will create another arrow function just like onError. In this function, we will essentially just log that we are now listening to incoming requests. This function is little tricky to understand and will be coded in the following way:

Adding error handling in server.js in MEAN Stack

4) Then, we will set up the port by calling the normalizePort function. In this function, we will pass 3000 as a string because of the value which we received from process.env.PORT would be a string.

Adding error handling in server.js in MEAN Stack

5) We will set up the node server and attach it to the listeners. First, we will attach the server for the error that might call when the error arises. We also attach the server with the listener for whenever we start listening and then we will start the server like as:

Adding error handling in server.js in MEAN Stack

6) Now, we had not quit the server, and we need to quit and restart it to see that in action. It is annoying to quit and restart the server constantly, and for this purpose, we will install an extra package. This package makes it easier and is used during development. We will use the following command to install the package:

Adding error handling in server.js in MEAN Stack

The nodemon stands for node monitoring. This is a powerful package that watches our nodejs file or our JavaScript file. If we change either in JavaScript or nodejs file, it will automatically restart the node server for us.

After installing it, we just have to use it to run our node server. The nodemon command will not work right now because we have not installed it globally on our machine. We have to make available it inside of this project scope, and for this purpose, we need to write a script in the scripts section of the package.json file. We will register a new script in the following way:

Adding error handling in server.js in MEAN Stack

Now, we will simply run the script using npm as:

Adding error handling in server.js in MEAN Stack

Now, if we make changes in our code, our node server will start automatically like this:

Adding error handling in server.js in MEAN Stack

Download Complete Project(Adding error handling.zip)







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA