The professional way to debugg an application that is running in Nodejs on the server side is as follows:
- First, we add the flag inspect in the command that runs our server js.
$ node --inpect file.js
If we need to adjust the port, or add it to an inspector of a client such as Visual Studio Code, or stop the inspector in the first line to be able to debut before the start we can see the official documentation and these could be the results:
$ node --inpect=0.0.0.0:9229 file.js $ node --inspect-brk file.js
There are two problems that we face when working in this way, the first is that if we want to make a change we must stop and restart our server, the second is that if the application dies we must restart our server again.
To solve this problem we will use two programs:
- Forever: your job will be to maintain and restart our nodejs server in case our application dies.
- Nodemon: it will be in charge of monitoring our application files in case we make any changes to it.
we install forever and nodemon
$ npm install -g email@example.com $ npm install -g firstname.lastname@example.org
our server will run as follows
$ forever -c 'nodemon --watch /opt/app --inspect=0.0.0.0:9229' file.js
With this we can develop faster and we will not waste time when working our service.
I hope you liked the content, if you have any questions or want to write about a related topic, please leave your comment, until next time.