Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Edit: Try <a href="http://github.com/kriszyp/nodules" rel="nofollow noreferrer">nodules</a> and their <code>require.reloadable()</code> function.</p> <p>My former answer was about why not to reload the <strong>process</strong> of Node.js and does not really apply here. But I think it is still important, so I leave it here.</p> <p>Node.js is <strong>evented IO</strong> and crafted specifically to avoid multiple threads or processes. The famous <a href="http://www.kegel.com/c10k.html" rel="nofollow noreferrer">C10k problem</a> 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.</p> <h2>What does <em>evented IO</em> mean?</h2> <p>To take your example: serving a page. Each time Node.js is about to serve a page, a callback is called by the <strong>event loop</strong>. The <strong>event loop</strong> 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 <strong>event handler</strong> - is called.</p> <p>And on the server side? Let's have a look at a simple HTTP server (source code example taken from <a href="http://nodejs.org/docs/" rel="nofollow noreferrer">Node.js documentation</a>)</p> <pre><code>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/'); </code></pre> <p>This first loads the http module, then creates an HTTP server and tells it to invoke the inner function starting with <code>function (request, response)</code> every time an HTTP request comes in, then makes the server listen to port 8124. This completes almost immediately so that <code>console.log</code> will be executed thereafter.</p> <p>Now Node.js <strong>event loop</strong> takes over. The application does not end but waits for requests. And voilà each request is answered with <code>Hello World\n</code>.</p> <h2>In a summary, don't restart Node.js, but let its <strong>event loop</strong> decide when your code has to be run.</h2>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload