How to run node js file in background on window server?

I created a node.js project and uploaded it to my window server to provide an api service for a mobile application. when opening a command prompt and type

node app.js 

It works correctly, but when I close the command line, my node.js server was stopped. How to make it still running when I close the invitation?

Ubuntu example I use the command

 nohup 

How to do it in a window

+5
source share
3 answers

You can run the process in the background using pm2

 pm2 start app.js --watch 

This will start the process and also look for changes in the file. Read more about watch flag

+6
source

Nodemon #ftw. In Windows Forever, it doesn’t actually look at files as often as they accidentally watch them, and pm2 restarts the server every time you load a page.

Each of these tools works the same way, and each is installed in the same way as in the accepted answer. For instance:.

 npm install nodemon -g 

This installs nodemon globally, and for use you can simply go to the project folder and type:

 nodemon 

(Assume your project has an app.js file). You can add the -w switch, as in Forever and pm2, however, if you just want to see all the files, you can omit this. To run nodemon in the background with Forever, you run this:

 forever nodemon --exitcrash 

Nodemon is good for development, especially on Windows, and Forever and pm2 are more for production on Linux.

+1
source

No, you can’t.

Even if you are creating a GUI program, you need to run it through the console.

And as soon as you close the command line. Your service will be stopped or terminated only at that moment. Since node creates the server itself during operation: http.createServer().listen(port) or app.listen(port) . Thus, this makes it independent in nature.

So, as soon as you close the command line on which the server is running, all the services stopped at that moment.

-1
source

Source: https://habr.com/ru/post/1257767/


All Articles