Basics of NodeJS

In this lesson, we’re going to get an introduction to NodeJS server-side programming in JavaScript.

We’ll cover the following

  • What is Node.js?
    • Why Node?
    • A sample Node JS server
    • How to install Node.js

What is Node.js?

The official Node.js website says, “As an asynchronous event-driven JavaScript runtime, Node is designed to build scalable network applications. In the following “hello world” example, many connections can be handled concurrently. Upon each connection, the callback is fired, but if there is no work to be done, Node will sleep.”

You’re probably thinking, “…???”

What they actually mean to say is that Node is a runtime environment that can run JavaScript outside of browsers. Also that servers written in Node can handle multiple connections at once, which makes them more efficient.

Why Node?

Node is great for software prototyping i.e., building incomplete versions of something that demonstrate what it is meant to do eventually. Node is also incredibly fast and highly scalable, which means that it is great to use in the industry.

Furthermore, it uses JavaScript, which is a language that most people are already familiar with, so writing in Node is easy. Also, the code written for Node is usually cleaner and more consistent. Lastly, there is a large ecosystem of open-source libraries available for Node.

A sample Node JS server

The most common example Hello World of Node.js is a web server:

const http = require('http')

const hostname = '127.0.0.1'
const port = 3000

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

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`)
})

This code first includes the Node.js http module.

Node.js has a fantastic standard library, including first-class support for networking.

The createServer() method of http creates a new HTTP server and returns it.

The server is set to listen on the specified port and host name. When the server is ready, the callback function is called, in this case informing us that the server is running.

Whenever a new request is received, the request event is called, providing two objects: a request (an http.IncomingMessage object) and a response (an http.ServerResponse object).

Those 2 objects are essential to handle the HTTP call.

The first provides the request details. In this simple example, this is not used, but you could access the request headers and request data.

The second is used to return data to the caller.

In this case with:

res.statusCode = 200

we set the statusCode property to 200, to indicate a successful response.

We set the Content-Type header:

res.setHeader('Content-Type', 'text/plain')

and we close the response, adding the content as an argument to end():

res.end('Hello World\n')

How to install Node.js

Node.js can be installed in different ways. This post highlights the most common and convenient ones.

Official packages for all the major platforms are available at https://nodejs.dev/download/.

One very convenient way to install Node.js is through a package manager. In this case, every operating system has its own.

Other package managers for MacOS, Linux, and Windows are listed in https://nodejs.dev/download/package-manager/

nvm is a popular way to run Node.js. It allows you to easily switch the Node.js version, and install new versions to try and easily rollback if something breaks, for example.

It is also very useful to test your code with old Node.js versions.

See https://github.com/nvm-sh/nvm for more information about this option.

In any case, when Node.js is installed you’ll have access to the node executable program in the command line.

Also Read – How to Install Node.js on Windows?


That’s it!
You have successfully completed the post.

Peace Out!

One comment

Leave a Reply

Your email address will not be published.