Any application that can be written in JavaScript will eventually be written in JavaScript.  —Atwood’S Law

In this 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?

Let’s see what Node actually 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. It’s 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 afterthought, it’s built right into the name.

A Basic Node HTTP Server

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 microservice. There is no server to deploy your code to. Your code is the server.

Where Node 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, it’s proven to be a very capable engine for these type of applications.
  • Command line applications:
  • Desktop applications
    • Skype.
    • Github desktop.
    • VSCode.
    • Slack.

Getting Started With Node

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

Node Package Manager (NPM)

It’s 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 code in other projects.
    • Easily use code that was written by others in their projects.
  • So, NPM is basically all about code sharing and re-usability.
  • But NPM is also about the composability of bigger applications using small packages.
  • NPM revolutionized the way JavaScript developers work.

What Is an NPM Package (a.k.a. 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:

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:
Module system import

3. NPM Registry

  • Installed via  npm install module_name into the 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

Creating basic server
Server running on port 3000

2. Run JavaScript Using Node

Running js file using Node
Output of JS file with Node

Summary

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

References



Source link

Write A Comment