Node.js – Introduction

Any application that can be written in JavaScript, will eventually be written in JavaScript.

Atwood’S LAW

In this blog post I will give a very light introduction to Node. There are tons of tutorials out there on the internet about Node, however, I wanted to have a very clear and to the point reference to the topic.

What is Node

Lets see, what actually Node is. The following points will give you a general idea:

  • Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine.
  • As an asynchronous event driven JavaScript runtime, Node is designed to build scalable network applications.
  • It is JavaScript on your back-end servers.
  • However, node offers a lot more than executing JavaScript on your server.
  • In fact the execution of JavaScript on the server is not done by Node at all. Its done with a virtual machine, VM like V8 or Chakra.
  • Node is just the coordinator. It’s the one who instructs a VM to execute your JS.
  • Node is better described as a wrapper around a VM like V8.
  • Node has built-in modules providing rich features through easy-to-use asynchronous APIs.

Why Node

Dave Bautista improvised Drax’s funniest line in ‘Avengers: Infinity War “Why is Gomora?”.

So, here is one of the reasons why you would like to use Node.

Small code is actually why Node is named Node. In Node we build simple, small, single process building blocks (nodes), that can be organized with good networking protocols to have them communicate with each other and scale up to build large distributed programs (microservices?).

Scaling a node application is not an after thought, its built right in to the name.

A basic Node HTTP Server

These 11 lines of code, along with the Node.js runtime, are all it takes to create a very basic API or a micro-service. There is no server to deploy your code to. Your code is the server.

Where NODE.js is commonly found

  • Microservices and APIs
    • We saw an example of this in the form of HttpServer earlier.
  • Serverless cloud functions
    • As a dynamic language runtime with a fast startup and low memory consumption, its proven to be a very capable engine for this type of applications.
  • Command line applications
    • Webpack
    • Gulp
    • ESLint
  • Desktop applications
    • Skype
    • Github desktop
    • VSCode
    • Slack

Getting started with Node

You can download installer for Node on this link. Once installed you can use an editor to start writing your node applications. Also have a look on the getting started guide which offers very nice and easy to follow tutorial.

Node Package Manager (NPM)

Its a very popular package manager and used very frequently in applications development. when you install node, NPM is also installed with it.

  • NPM enables JavaScript developers to do three main things:
    • Share code with other developers.
    • Re-use own code in other projects.
    • Use code written by others in their projects.
  • So, NPM is basically all about code sharing and re-usability.
  • But NPM is also about composability of bigger applications using small packages.
  • NPM revolutionized the way JavaScript developers work.

What is an NPM Package (aka module)

  • The name package is what npm uses to label the bits of reusable code.
  • A module package is basically a folder that contains scripts that can be run by Node.

Three sources of Node modules

1. Built-in Modules

These modules come pre-packaged with Node. There are required()’d with a simple identified:

var fs = require(‘fs’);

  • A sample of built-in modules include:
    • fs
    • http
    • crypto
    • os

2. Your Project’s files

  • Each .js file is its own module
  • A great way to modularize your application’s code
  • Each file is require()’d with file system-like semantics:

3. NPM Registry

  • Installed via “npm install module_name” into “node_modules” folder.
  • Are require()’d via simple string identifiers, similar to built-ins
    • var request = require(‘request’);
  • Can require() individual files from within a module, but be careful!
  • Some modules provide command line utilities as well
    • Install these modules with “npm install –g module_name

Examples

1. Create a basic server

2. Run JavaScript using node

Summary

Node is a great environment. Its simple, powerful and have a great community behind it. This was a very basic introduction to Node and I will keep writing about it in my upcoming posts. Till next time, happy coding.

References