title: Single threaded and Event Loop in Node.js
body: <p>First of all, I am starter trying to understand what is Node.Js. I have two questions. <br /></p>
  
  <p><strong>First Question</strong> <br/>
  From <a href="http://debuggable.com/posts/understanding-node-js:4bd98440-45e4-4a9a-8ef7-0f7ecbdd56cb" rel="noreferrer">the article</a> of Felix, it said "there can only be one callback firing at the same time. Until that callback has finished executing, all other callbacks have to wait in line".</p>
  
  <p>Then, consider about the following code (copied from nodejs official website)</p>
  
  <pre><code>var http = require('http');
  http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
  }).listen(8124, "127.0.0.1");
  </code></pre>
  
  <p>If two client requests are received simultaneously, it means the following workflow:</p>
  
  <ol>
  <li>First http request event received, Second request event received.</li>
  <li>As soon as first event received, callback function for first event is executing.</li>
  <li>At while, callback function for second event has to be waiting.</li>
  </ol>
  
  <p>Am I right? If I am right, how <strong>Node.js</strong> control if there are thousands of client request within very short-time duration. </p>
  
  <p><strong>Second Question</strong> <br />
  The term "Event Loop" is mostly used in Node.js topic. I have understood "Event Loop" as the following from <a href="http://www.wisegeek.com/what-is-an-event-loop.htm" rel="noreferrer">http://www.wisegeek.com/what-is-an-event-loop.htm</a>;</p>
  
  <blockquote>
    <p>An event loop - or main loop, is a construct within programs that
    controls and dispatches events following an initial event.</p>
    
    <p>The initial event can be anything, including pushing a button on a
    keyboard or clicking a button on a program (in Node.js, I think the
    initial events will be http request, db queries or I/O file access).</p>
    
    <p>This is called a loop, not because the event circles and happens
    continuously, but  because the loop prepares for an event, checks the
    event, dispatches an event and repeats the process all over again.</p>
  </blockquote>
  
  <p>I have a conflict about second paragraph especially the phrase"<strong>repeats the process all over again</strong>". I accepted that the above <strong>http.createServer</strong> code from above question is absolutely "event loop" because it repeatedly listens the http request events. </p>
  
  <p>But I don't know how to identify the following code as whether event-driven or event loop. It does not repeat anything except the callback function fired after db query is finished.</p>
  
  <pre><code>database.query("SELECT * FROM table", function(rows) {
    var result = rows;
  });
  </code></pre>
  
  <p>Please, let me hear your opinions and answers.</p>