Introduction
In this module, we will introduce you to node.js, its features, advantages and its supports.
Overview
Node.js is a cross-platform runtime environment for server-side and networking applications. Node.js applications are written in JavaScript, and can be run within the Node.js runtime on OS X, Microsoft Windows and Linux with no changes.
Node.js applications are designed to maximize throughput and efficiency, using non-blocking I/O and asynchronous events. It is commonly used for real-time applications due to its asynchronous nature. Node.js internally uses the Google V8 JavaScript engine to execute code, and a large percentage of the basic modules are written in JavaScript. Node.js contains a built-in asynchronous I/O library for file, socket, and HTTP communication, which allows applications to act as a Web server without software such as Apache HTTP Server or IIS.
The goal of Node.js is to offer an easy and safe way to build high performance and scalable network applications in JavaScript. Node.js is part of the Server Side JavaScript environment and extends the JavaScript API to offer server side features. Node.js is able to do this by handling many client connections at the same time. The Node.js API can be extended using the CommonJS module system.
Threading
Threading
Traditional Web-serving techniques require each connection (request) to create a new thread, taking up system RAM and eventually maxing-out at the amount of RAM available. Node.js is different. It operates on a single-thread, using non-blocking I/O calls, allowing it to support tens of thousands of concurrent connections, without worrying about RAM limitations and the cost of context-switching between threads. The design of sharing a single-thread between all the requests means it can be used to build highly concurrent applications. The design goal of a Node.js application is that any function performing an I/O must use a callback
Package management
Package management
Npm is the package manager that comes pre-installed with the Node.js server platform. It is used to install Node.js programs from the npm registry. By organizing the installation and management of third-party Node.js programs, it helps developers build faster. npm is not to be confused with the commonJS require() statement. It is not used to load code: instead, it is used to install code and manage code dependencies from the command line. The packages found in the npm registry can range from simple helper libraries like underscore.js to task runners like grunt.js.
Unified API
Node.js combined with a browser, a document DB (such as MongoDB or CouchDB) and JSON offers a unified JavaScript development stack. With the increased attention to client-side frameworks and the adaptation of what were essentially server-side development patterns like MVC, MVP, MVVM, etc., Node.js allows the reuse of the same model and service interface between client-side and server-side.
How does it work?
The main idea of Node.js: use non-blocking, event-driven I/O to remain lightweight and efficient in the face of data-intensive real-time applications that run across distributed devices.
How it works under-the-hood is pretty interesting. Compared to traditional web-serving techniques where each connection (request) spawns a new thread, taking up system RAM and eventually maxing-out at the amount of RAM available, Node.js operates on a single-thread, using non-blocking I/O calls, allowing it to support tens of thousands of concurrent connections.
Tools
- Desktop IDEs
- Atom (free open-source)
- Nodeclipse Enide Studio (free open-source, Eclipse-based)
- JetBrains WebStorm (commercial)
- JetBrains IntelliJ IDEA (commercial)
- Microsoft Visual Studio with Node.js Tools for Visual Studio (commercial)
- Microsoft Visual Studio with TypeScript (commercial)
- NoFlo – flow-based programming environment integrated with GNOME APIs
- Online code editors
- Codenvy IDE (cloud service)
- Cloud9 IDE (cloud service)
- Runtimes and debuggers
- Microsoft WebMatrix (free)
- Microsoft Visual Studio (commercial) with Node.js Tools for Visual Studio (free)
NPM: The Node Package Manager
When discussing Node.js, one thing that definitely should not be omitted is built-in support for package management using the NPM tool that comes by default with every Node.js installation. The idea of NPM modules is quite similar to that of Ruby Gems: a set of publicly available, reusable components, available through easy installation via an online repository, with version and dependency management.
A full list of packaged modules can be found on the NPM website https://npmjs.org/ , or accessed using the NPM CLI tool that automatically gets installed with Node.js. The module ecosystem is open to all, and anyone can publish their own module that will be listed in the NPM repository. A brief introduction to NPM (a bit old, but still valid) can be found at http://howtonode.org/introduction-to-npm.
Some of the most popular NPM modules today are:
Server-side web applications
Node.js with Express.js can also be used to create classic web applications on the server-side. However, while possible, this request-response paradigm in which Node.js would be carrying around rendered HTML is not the most typical use-case. There are arguments to be made for and against this approach. Here are some facts to consider:
Pros:
Server-side web applications w/a relational db behind
Comparing Node.js with Express.js against Ruby on Rails, for example, there is a clean decision in favour of the latter when it comes to relational data access. Relational DB tools for Node.js are still in their early stages; they’re rather immature and not as pleasant to work with. On the other hand, Rails automagically provides data access setup right out of the box together with DB schema migrations support tools and other Gems (pun intended). Rails and its peer frameworks have mature and proven Active Record or Data Mapper data access layer implementations, which you’ll sorely miss if you try to replicate them in pure JavaScript. Still, if you’re really inclined to remain JS all-the-way (and ready to pull out some of your hair), keep an eye on Sequelize and Node ORM2—both are still immature, but they may eventually catch up.
We've discussed Node.js from theory to practice, beginning with its goals and ambitions, and ending with its sweet spots and pitfalls. When people run into problems with Node, it almost always boils down to the fact that blocking operations are the root of all evil—99% of Node misuses come as a direct consequence. Remember: Node.js was never created to solve the compute scaling problem. It was created to solve the I/O scaling problem, which it does really well. Why use Node.js? If your use case does not contain CPU intensive operations nor access any blocking resources, you can exploit the benefits of Node.js and enjoy fast and scalable network applications. Welcome to the real-time web.
In next modules, we will be having a look for how to use it in our applications.
Some of the most popular NPM modules today are:
- express - Express.js, a Sinatra-inspired web development framework for Node.js, and the de-facto standard for the majority of Node.js applications out there today.
- connect - Connect is an extensible HTTP server framework for Node.js, providing a collection of high performance “plugins” known as middleware; serves as a base foundation for Express.
- socket.io and sockjs - Server-side component of the two most common websockets components out there today.
- Jade - One of the popular templating engines, inspired by HAML, a default in Express.js.
- mongo and mongojs - MongoDB wrappers to provide the API for MongoDB object databases in Node.js.
- redis - Redis client library.
- coffee-script - CoffeeScript compiler that allows developers to write their Node.js programs using Coffee.
- underscore (lodash, lazy) - The most popular utility library in JavaScript, packaged to be used with Node.js, as well as its two counterparts, which promise better performance by taking a slightly different implementation approach.
- forever - Probably the most common utility for ensuring that a given node script runs continuously. Keeps your Node.js process up in production in the face of any unexpected failures.
Where Node.js can be used?
Server-side web applications
Pros:
- If your application doesn’t have any CPU intensive computation, you can build it in Javascript top-to-bottom, even down to the database level if you use JSON storage Object DB like MongoDB. This eases development (including hiring) significantly.
- Crawlers receive a fully-rendered HTML response, which is far more SEO-friendly than, say, a Single Page Application or a websockets app run on top of Node.js.
- Any CPU intensive computation will block Node.js responsiveness, so a threaded platform is a better approach. Alternatively, you could try scaling out the computation.
- Using Node.js with a relational database is still quite a pain (see below for more detail). Do yourself a favour and pick up any other environment like Rails, Django, or ASP.Net MVC if you’re trying to perform relational operations.
Where Node.js should not be used?
Server-side web applications w/a relational db behind
Comparing Node.js with Express.js against Ruby on Rails, for example, there is a clean decision in favour of the latter when it comes to relational data access. Relational DB tools for Node.js are still in their early stages; they’re rather immature and not as pleasant to work with. On the other hand, Rails automagically provides data access setup right out of the box together with DB schema migrations support tools and other Gems (pun intended). Rails and its peer frameworks have mature and proven Active Record or Data Mapper data access layer implementations, which you’ll sorely miss if you try to replicate them in pure JavaScript. Still, if you’re really inclined to remain JS all-the-way (and ready to pull out some of your hair), keep an eye on Sequelize and Node ORM2—both are still immature, but they may eventually catch up.
Conclusion
We've discussed Node.js from theory to practice, beginning with its goals and ambitions, and ending with its sweet spots and pitfalls. When people run into problems with Node, it almost always boils down to the fact that blocking operations are the root of all evil—99% of Node misuses come as a direct consequence. Remember: Node.js was never created to solve the compute scaling problem. It was created to solve the I/O scaling problem, which it does really well. Why use Node.js? If your use case does not contain CPU intensive operations nor access any blocking resources, you can exploit the benefits of Node.js and enjoy fast and scalable network applications. Welcome to the real-time web.
In next modules, we will be having a look for how to use it in our applications.