Node.js — What? Why? and How?

Dananjaya Sandakalum
4 min readApr 14, 2021

--

Node.js is an open-source and cross-platform JavaScript runtime environment. It is currently the most popular tool for almost any kind of project!

Node.js runs the V8 JavaScript engine, the core of Google Chrome, outside of the browser. Since millions of front-end developers who write JavaScript for the browser can now write server-side code in addition to client-side code without having to learn a new language, Node.js has a distinct advantage.

History of Node.js

Node.js is only eleven years old. In the world of technology, eleven years isn’t a long time. Compared to Javascript, which has been around 24 years, Node.js is very young. But within those few years, Node.js has become a huge thing in web development.

The first form of npm is created in 2009. Later 2010 Express and Socket.io was born. By 2011, larger companies like Paypal, LinkedIn, and, Uber start adopting Node.js.

In 2015, The Node.js Foundation was born. The OpenJS Foundation is made up of 32 open-source JavaScript projects including Appium, Dojo, Electron, jQuery, Node.js, and Webpack.

Now it’s 2021 and Node.js 15 has already been released.

Benefits of Node.js

The strengths and weaknesses of Node.js have sparked intense debate with experts.

Using Node.js for backend, you automatically get all the pros of full-stack JavaScript development. As a result, the team is much more flexible, development takes less time, and you end up with fast and stable applications.

Node.js is a lightning-fast programming language. The engine used in the Node.js implementation was designed specifically for the Chrome browser. And also, Non-blocking Input/Output and asynchronous request handling made Node.js capable of processing requests without any delays.

How to install Node.js

Node.js can be set up in multiple of ways.

Official Node.js packages are available at https://nodejs.org/en/download/.

A package manager is a very simple way to install Node.js. Each operating system has its own in this case. You can download package manager for Windows through here : https://nodejs.org/en/#home-downloadhead

Screenshot of www.nodejs.org website

NPM is the package module which helps javascript developers load dependencies effectively. To load dependencies we just have to run a command in command prompt:

> npm install

This command is finding a json file named as package.json in root directory to install all dependencies defined in the file.

{
"name": "ApplicationName",
"version": "0.0.1",
"description": "Application Description",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"repository": {
"type": "git",
"url": "https://github.com/npm/npm.git"
},
"dependencies": {
"express": "~3.0.1",
"sequelize": "latest",
"q": "latest",
"tedious": "latest",
"angular": "latest",
"angular-ui-router": "~0.2.11",
"path": "latest",
"dat-gui": "latest"
}
}

This is a most common and simple example of what a package.json looks like. The most important things in your package.json are name and version. Those are required, and your package won’t install without them. The name and version together form an identifier that is assumed to be unique. Changes to the package should come along with changes to the version.

If you want to install the latest version of a file, you just have to put “latest” in place of the version name in dependencies section. Tilde(~) is used to tell “Approximately equivalent to version”.

npm list

Using this you can find version of all installed npm packages, including their dependencies. You can also open the package.json file directly, but this will require some visual inspection.

/*server.js*/

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer(function(req, res) {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});

server.listen(port, hostname, function() {
console.log('Server running at http://'+ hostname + ':' + port + '/');
});

This is a simple “Hello World” program using Node.js. To run this file, first, you have to save this file on your computer. (Use server.js as the name) Now you have to open the Command Prompt on your computer. (How to open the command line interface on your computer depends on the OS.) Then navigate to the folder that contains the file “server.js”.

> C:\Users\Your Name>_

Now you can run “server.js” file using this command.

> C:\Users\Your Name>node server

Open a browser and enter url http://127.0.0.1:3000/. The browser will display Hello World message on the screen.

--

--

Dananjaya Sandakalum
Dananjaya Sandakalum

Written by Dananjaya Sandakalum

Hi, I’m Sandakalum – a Software Engineer, and freelance graphic designer. I specialize in MERN stack front-end and WordPress development.

No responses yet