Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs without stopping

Is there a way I can make nodejs reload everytime it serves a page?

I want to do this during the dev cycle so I can avoid having to shutdown & startup on each code change?

like image 259
Tyler Gillies Avatar asked Aug 01 '10 16:08

Tyler Gillies


People also ask

How do I run nodeJS forever?

js application locally after closing the terminal or Application, to run the nodeJS application permanently. We use NPM modules such as forever or PM2 to ensure that a given script runs continuously. NPM is a Default Package manager for Node.

Is nodeJS still relevant 2022?

Node. js development has become very popular over the last four years and continues to stand the competition in 2022 making startups worldwide choose it over other available options.

Why do you use forever with nodeJS?

Forever is an npm module that ensures a Node. js script continuously runs in the background on the server. It's a helpful CLI tool for the production environment because it helps manage the Node applications and their processes.


2 Answers

Edit: Try nodules and their require.reloadable() function.

My former answer was about why not to reload the process of Node.js and does not really apply here. But I think it is still important, so I leave it here.

Node.js is evented IO and crafted specifically to avoid multiple threads or processes. The famous C10k problem asks how to serve 10 thousand clients simultaneously. This is where threads don't work very well. Node.js can serve 10 thousand clients with only one thread. If you were to restart Node.js each time you would severely cripple Node.js.

What does evented IO mean?

To take your example: serving a page. Each time Node.js is about to serve a page, a callback is called by the event loop. The event loop is inherent to each Node.js application and starts running after initializations have completed. Node.js on the server-side works exactly like client-side Javascript in the browser. Whenever an event (mouse-click, timeout, etc.) happens, a callback - an event handler - is called.

And on the server side? Let's have a look at a simple HTTP server (source code example taken from Node.js documentation)

var http = require('http');

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World\n');
}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');

This first loads the http module, then creates an HTTP server and tells it to invoke the inner function starting with function (request, response) every time an HTTP request comes in, then makes the server listen to port 8124. This completes almost immediately so that console.log will be executed thereafter.

Now Node.js event loop takes over. The application does not end but waits for requests. And voilà each request is answered with Hello World\n.

In a summary, don't restart Node.js, but let its event loop decide when your code has to be run.

like image 118
nalply Avatar answered Sep 30 '22 06:09

nalply


Found Nodemon, exactly what I wanted: https://github.com/remy/nodemon

like image 34
Tyler Gillies Avatar answered Sep 30 '22 06:09

Tyler Gillies