In my last post, I gave a quick overview of Node.js and showed you how to install and smoke test it. Now let’s dive deeper and learn what it provides and how it works.
Google’s V8 JavaScript engine provides the underlying VM for executing JavaScript code. Since V8 is typically embedded in a browser, it does not expose any real notion of FileSystems, Processes and I/O streams that are fundamental to a server-side application. Node.js provides core APIs that wrap the standard POSIX functions available on all modern Unixy systems, along with higher-level APIs for HTTP/S support.
Node.js is designed to be an asynchronous system: your application code executes only on a single thread and no blocking I/O is allowed. Instead, Node.js uses an event loop to process I/O events via the libev C library.
Let’s dive into the Node code to see how it initializes itself. This writeup is current as of 0.4.2 or 3/14/2011 but obviously things will change over time.
Node initializes itself in the node::Start method in node.cc. node::Start
performs the housekeeping necessary to use V8 and libev in a well-behaved process. A brief walkthru:
node::Load
to bootstrap the Node.js APIs, see belownode::Load performs the actual Node.js API bootstrap in three stages:
lib/
when require(module)
is called.Once the user’s code has been loaded, we should have set up any listening sockets, timers, etc. The entire node::Load function unwinds and we enter the event loop, waiting for I/O or Timer events to execute code.
Thanks for staying with me this far – I hope you learned something new. Next up, I’ll show you how to configure and run a full stack application on node.js using MongoDB and Express!