How to make a node.js application run permanently?

Its easy to make node,js application run permanently in the background please follow these steps


You could install forever using npm like this: (I hope you installed node already or install Node.js)

sudo npm install -g forever


Create an example file: and add content

sudo nano server.js

var http = require('http');

                         var server = http.createServer(function (request, response) {  

                         response.writeHead(200, {"Content-Type": "text/html"});
                   response.end("<h3>Node webserver running</h3>\n");
});

server.listen(3000);

console.log("Node.js is listening on port 3000");  // JavaScript Document


and content and save file

You can edit the file and get results directly in your browser.

You can use filezilla or any editor to edit the file. Run this command to run the file:

forever start --minUptime 1 --spinSleepTime 1000 -w server.js




Go to the browser to test file

http://YOURIP:3000/

Explanation:

And then start your application with:

forever server.js

Or as a service:

forever start server.js

Forever restarts your app when it crashes or stops for some reason. To restrict restarts to 5 you could use:

forever -m5 server.js

To list all running processes:

forever list
Note the integer in the brackets and use it as following to stop a process:

forever stop 0
Restarting a running process goes:

forever restart 0
If you're working on your application file, you can use the -w parameter to restart automatically whenever your server.js file changes:

forever -w server.js


view Running node

ps aux | grep node

you want to kill Node (ex:5655)  four digit number you will see when type ps aux | grep node

afer kill node whichever you want

kill -9  5655

3 comments:

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...