JavaScript is a dynamic, object-oriented, and functional scripting language. One of the features that make it win over Java Applets in the browser scripting war decades ago, it was its lightness and non-blocking event loop.

Bocking means that when one line of code is executing the rest of it is locked waiting to finish. On the other hand, non-blocking gives to each line of code a shot and then through callbacks it can come back when an event happens. Programming languages that are blocking (Java, Ruby, Python, PHP, …) overcomes concurrency using multiple threads of execution while JavaScript handles it using non-blocking event loop in a single thread.

Some companies like Paypal moved from Java backend to NodeJS and reported a increased performance, lower average response times, and development speed gains. Similarly happens to Groupon that came from Java/Rails monoliths.

1. 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 a set of publicly available, reusable components, available through easy installation via an online repository, with version and dependency management.

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.

The best way to manage locally installed npm packages is to create a package.json file.

A package.json file allows you to:

documentat what packages your project depends on

it allows you to specify the versions of a package that your project can use using semantic versioning rules

makes your build reproducible which means that its way easier to share with other developers.

To specify the packages your project depends on, you need to list the packages you’d like to use in your package.json file. There are 2 types of packages you can list:

"dependencies": these packages are required by your application in production

"devDependencies": these packages are only needed for development and testing

JavaScript

1

2

3

4

5

6

7

8

9

10

{

"name":"my_package",

"version":"1.0.0",

"dependencies":{

"my_dep":"^1.0.0"

},

"devDependencies":{

"my_test_framework":"^3.1.0"

}

}

The above package.json specify that the app uses any version of the package my_dep that matches major version 1 in production, and requires any version of the package my_test_framework that matches major version 3, but only for development.

This is a tool to manage node processes during development. With nodemon you can start a node process and it keeps it running for. It utilizes fsevents to hook into filesystem changes and it restarts the node process on each file change.

You can install it using npm using the following command. I like to install it globally so I can use it for all projects, but you can remove the -g to install it locally instead.

MS DOS

1

npminstall -gnodemon

Now instead of using node server.js to run your application, you can use nodemon server.js. It will watch for any changes in your application and automatically restart your server for you.

3. Node inspector

Node Inspector is a debugger interface for Node.js applications that uses the Blink Developer Tools. The really cool thing is that it works almost exactly as the Chrome Developer Tools.

MS DOS

1

npminstall -gnode-inspector

You should be a flavour of Chrome browser (Chrome, Chromium, etc) installed.

Once it is installed, you can run it using the following command. This will start the debugger and open your browser.

MS DOS

1

node-debugserver.js

Can you combine nodemon with node inspector ? You would start your server with nodemon --debug server.js and then you’ll need to run node-inspector in a separate terminal window unless you push nodemon to the background.

The platform and core framework were designed around an event-driven, non-blocking I/O model and constructing a trivial server is as simple as the following script:

JavaScript

1

2

3

4

5

6

7

varhttp=require('http');

http.createServer(function(req,res){

res.writeHead(200,{'Content-Type':'application/json'});

res.end('{ "message": "Hello World" }');

}).listen(3000,'127.0.0.1');

console.log('Server listening at http://127.0.0.1:3000/');

The server can be launched by executing the script via Node:

JavaScript

1

2

$node server.js

Server listening at http://127.0.0.1:3000/

Contributing to Node’s phenomenal growth was an excellent package management system built on lessons learned from other communities. Creating, publishing, and installing a Node module or collection of related modules as a package is simple and fast.

Packages contain their own local copy of installed modules, easing deployment since there is no requirement to install to any common file system locations (and resolve potential version conflicts).

All that is required for a package is a package.json file that contains a bit of metadata about a package and its dependencies. A minimal package.json file only requires a name and version.

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

{

"name":"helloworldapp",

"version":"0.0.0",

"private":true,

"scripts":{

"start":"node ./bin/www"

},

"dependencies":{

"body-parser":"~1.13.2",

"cookie-parser":"~1.3.5",

"debug":"~2.2.0",

"express":"~4.13.1",

"jade":"~1.11.0",

"morgan":"~1.6.1",

"serve-favicon":"~2.3.0"

}

}

Nodejs benefits:

Lightweight HTTP server processes — the Node platform is based on Google’s well-regarded open source, high performance V8 engine, which compiles JavaScript to native machine code on supported systems; this machine code undergoes dynamic optimization during runtime. The V8 engine is highly tuned for fast startup time, small initial memory footprint, and strong peak performance.

Highly scalable — the Node platform was designed from the onset for end-to-end asynchronous I/O for high scalability.

Lightweight for developers — there is minimal ceremony involved in creating and consuming packages, which encourages a high degree of modularization and lightweight, narrowly-focused packages independently versioned and published.

Where Node.js Can Be Used ?

Server-side web applications / REST APIs

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.

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

Cons:

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. 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 Shouldn’t Be Used?

Server-side web application with a relational database behind Relational DB tools for Node.js are still in their early stages; they’re rather immature and not as pleasant to work with. 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.

Heavy server-side computation/processing – When it comes to heavy computation, Node.js is not the best platform around. No, you definitely don’t want to build a Fibonacci computation server in Node.js. In general, any CPU intensive operation annuls all the throughput benefits Node offers with its event-driven, non-blocking I/O model because any incoming requests will be blocked while the thread is occupied with your number-crunching. Node.js is single-threaded and uses only a single CPU core. When it comes to adding concurrency on a multi-core server, there is some work being done by the Node core team in the form of a cluster module [ref: http://nodejs.org/api/cluster.html]. You can also run several Node.js server instances pretty easily behind a reverse proxy via nginx. With clustering, you should still offload all heavy computation to background processes written in a more appropriate environment for that, and having them communicate via a message queue server like RabbitMQ.

Conclusion

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.