Make nodemon auto-restart a program on crash without waiting for file changes at custom error?
Asked Answered
B

3

7

I'm building an E-commerce site, where there's an Authentication system.

I noticed that if the client login with a wrong user or password, the backend/server that works with nodemon will crach and hang in there crashed till i restart manually nodemon. This is example output error of the nodemon crash:

[nodemon] app crashed - waiting for file changes before starting...

node:internal/errors:464 ErrorCaptureStackTrace(err);

^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Ofcourse, when server crashes, client can no more access or do login again till server restarts. After some googling, i found this question and this repository that fix my problem but particulary and not as expected precisely, i dont want nodemon to restart forever on any error that occure ofcourse, but only with specifics errors that i set them -like Authentication errors as i mentionned above-.

So, my idea/question is: is there anyway to get nodemon restarts by itself in some cases of failures or errors (NOT ALL)?

Basinger answered 19/7, 2022 at 14:46 Comment(0)
G
6

Seems like you a referring to a production situation, and nodemon is a development node server, which is not intended for use in production, as the intro states:

nodemon is a tool that helps develop Node.js based applications by automatically restarting the node application when file changes in the directory are detected.

You should use node.js in production, instead of nodemon.

For managing your node server in production, you could use a process manager like PM2..

That said, an authentication server that crashes every time a user uses a wrong password seams very ineffective in handling a common use case. So I would advise to start with fixing the root cause, which is the buggy server, and then for recovery from incidental crashes use something like PM2.

PS: The error you are getting looks like an express error you get when you send a response (in this case an error response) without exiting the function e.g. by using return. Because you are not returning, another res.send is called, which causes the 'ERR_HTTP_HEADERS_SENT' error. See this answer.

Gristmill answered 20/7, 2022 at 15:51 Comment(3)
hmm so you think there's no chance i can modify nodemon package to work the way i expect it to ?Basinger
Well, I think you not even should try to do that. Nodemon monitors your source files for changes during development and reload on change. That's why it says 'waiting for file change' after a crash. In development, usually you made a mistake, correct it, save the source file, nodemon notices that and reloads. Since do not change source files in production (I certainly hope), this functionality has no use in a production environment and therefore you should use node.js. Which will also crash and not recover in your situation.Gristmill
This is the correct answer and should be the most upvotedApplicative
L
5

If you're using linux and have SystemD, you can create a service that launches nodemon and set Restart=on-failure in the .service file. Use nodemon --exitcrash if you want service to auto restart after a crash.

[Unit]
Description=myService 

[Install]
WantedBy=multi-user.target

[Service]
ExecStart=/usr/local/bin/nodemon /apps/myapp/app.js --watch /apps/myapp/app.js --exitcrash
Type=simple
User=root
Group=root
WorkingDirectory=/apps/
Restart=on-failure

https://wiki.debian.org/systemd/Services

Lindner answered 27/6, 2023 at 14:34 Comment(0)
P
0

This is really bad since it can send your program into a loop of restarting, but if you really want it, replace app.js with your file's name and try this:

nodemon -x 'node app.js || copy /b app.js +,,'

Linux version:

nodemon -x 'node app.js || touch app.js'

Next time try a little googleing before you ask since it is most likely faster.

Psychosomatics answered 19/7, 2022 at 16:44 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Devitalize

© 2022 - 2024 — McMap. All rights reserved.