Introduction To Nodejs

Ashwani Goswami
AltCampus
Published in
3 min readDec 20, 2018

Here’s a formal definition as given on the official Node.js website:

Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine.

it is a server-side platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications.

Why nodejs?

Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. If you are a beginner, you might hear these words(event-driven, non-blocking I/O) most of the time. so, let’s talk about them:

Event-driven

Event-driven means that the server only reacts when an event occurs. This allows us to create high performance, highly scalable, “real-time” applications.

Non-blocking I/O

In non-blocking, every I/O call must take a callback, whether it is to retrieve information from disk, network or another process. so, non-blocking means Asynchronous. that’s why, In nodejs, the server can handle multiple requests at the same time. While In Blocking I/O, there will be only one request at a time, you can not initiate another request until the response of the first request comes. and nodejs is built for the real-time application, that’s why it uses non-blocking I/O model.

The Node Event loop

Node.js applications use “Single Threaded Event Loop Model” architecture to handle multiple clients at the same type. These features are key factors to make real-time web applications. the node event loop is the same as a javascript event loop. In javascript, the function in the callback queue waits for the call stack to be empty because only one statement can execute at a time. this is taken care of by event loop. and this is how asynchronous function works in javascript.

NPM (Node Package Manager)

NPM (Node Package Manager) is the default package manager for Node.js. it gets installed into the system with the installation of Node.js. The required packages and modules in Node project are installed using NPM. it can install all the dependencies of a project through the package.json file using the command

npm init -y

Npm has the many packages that you can use in your app to make your development faster and efficient. Installing modules using Npm is not a big deal. There is a simple syntax to install any Node.js module:

npm install <Module Name>
eg.npm install express

To use this module in your application, you have to require this module in your js file, like

const express = require('express')

What require does?

Require is a function which takes an argument “path” of any module or it can be your own file. As you can see above, we require “express ” in our app, so it imported from our node_module file. if there is no such path in node modules, it will throw an error that module not found. so basically require function takes a path, search for the module and return module.exports.

these are the basics of nodejs, now we can write a simple code to print “hello world” in nodejs;

“Hello World” in nodejs:

So, all we need to do is:

make a file app.js and write a single line code in it:

console.log("Hello World")

now, go to the node terminal, (in your current directory) and type:

node app.js

😄 wow, you did this, you have written hello world in nodejs.

You can follow me on twitter.

--

--