6

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?

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
Tyler Gillies
  • 1,857
  • 4
  • 22
  • 31
  • It seems you mean to hot reload edited source code during development cycle and not reload the whole process. – nalply Aug 12 '10 at 10:29
  • While this may not answer your question, it does explain the core concept of Node.js and does provide the module you need: http://www.devthought.com/2012/01/29/staying-up-with-node-js/ – Sơn Trần-Nguyễn Feb 27 '12 at 01:09
  • Does this do what you're looking for? http://phpasptutorialize.wordpress.com/2010/09/14/how-to-configure-node-js-script-auto-restart/ – Garrett Serack Dec 09 '10 at 18:32

5 Answers5

10

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.

Community
  • 1
  • 1
nalply
  • 26,770
  • 15
  • 78
  • 101
  • It's not for production, its for dev, so i don't have to restart when i edit code – Tyler Gillies Aug 12 '10 at 01:53
  • 1
    http://stackoverflow.com/questions/1972242/auto-reload-of-files-in-node-js/1975294#1975294 but: Node is still bleeding edge and since then a lot has changed. – nalply Aug 12 '10 at 10:27
6

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

Tyler Gillies
  • 1,857
  • 4
  • 22
  • 31
0

I personnaly use spark2 (the fork) and will switch to cluster as soon as i found the time to test it. Among other things, those 2 will listen to file changes and reload the server when appropriate, which seems to be what you're looking for.

wdavidw
  • 161
  • 1
  • 4
0

There are many apps for doing this, but I think the best is Cluster, since you have 0 downtime for your server. You can also set multiple workers with it or manually start/stop/restart/show stats with the cli REPL functionality.

alessioalex
  • 62,577
  • 16
  • 155
  • 122
-3

No. Node.js should always run. I think you may have misunderstood the concept of nodejs. In order for nodejs to serve pages, it has to be its own server. Once you start a server instance and start listening on ports, it will run until you close it.

Is it possible that you've called a function to close the server instead of just closing a given stream?

Tor Valamo
  • 33,261
  • 11
  • 73
  • 81
  • I want the entire process to restart itself on each request. shotgun for sinatra does this http://ruby.about.com/od/sinatra/a/sinatra5.htm – Tyler Gillies Aug 02 '10 at 18:49
  • It would help if you also told us why you'd want this. I see no practical use. – Tor Valamo Aug 03 '10 at 03:03
  • there's no way around it. just be glad you don't use stuff like C where you have to compile your app every time you make changes. – Tor Valamo Aug 12 '10 at 13:12
  • 2
    @TorValamo Not really a useful attitude in these comments. The practical use is quite obvious if you read about shotgun (link provided in the first comment). It's also completely wrong to state that "there's no way around it", since several other answers here actually provides solutions to the problem. – Jakob Oct 31 '11 at 17:51
  • @Jakob - you may also have noted that this post is from a year ago, when node was NOT PHP. I haven't programmed in a lot of months, so I have no idea where Node is at right now. But you have to see it from the perspective at the time it was posted... – Tor Valamo Nov 02 '11 at 03:46