Darkpsinight
Darkpsinight

Reputation: 649

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

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)?

Upvotes: 8

Views: 16742

Answers (4)

Jay
Jay

Reputation: 323

As other posters said, nodemon is meant for development and not production.

But here is an answer anyway. :-)

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

This will utilize nodemon to auto-restart your application after a crash, without any file changes. By default nodemon requires file changes to trigger a restart, and there isn't a way to disable it. So you have to trick it by using "touch" to update the file timestamp. This counts as a "file change" and satisfies nodemon.

Btw if you have a complex node command, it will work with this trick, e.g. one of mine is

nodemon -w ./dist/index.js -x 'node -r module-alias/register ./dist --env=production || touch ./dist/index.js'

Upvotes: 0

sceptre357
sceptre357

Reputation: 61

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

Upvotes: 6

Gabe
Gabe

Reputation: 2626

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.

Upvotes: 7

Mikachu
Mikachu

Reputation: 157

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.

Upvotes: 1

Related Questions