NodeJS – Getting Started

wordpress featured image

NodeJS is a backend JavaScript based runtime environment, which is cross-platform and executes JavaScript outside of the web browser. It is highly scalable as it’s been designed with non-blocking, event-driven architecture. While it was initially designed for real-time push based architecture, it has gotten quite popular in a lot of aspects due to it’s lightweight as well as scalable nature.

To get started with NodeJS, we need to install the Node.js platform. The Node.js platform distribution includes the Node Package Manager (npm),

For downloading and installation, check out:

Once you have Node installed, check by running:

> node --version
// prints 'v18.12.0' for my system

Creating ‘Hello World’ Project

Navigate into your work folder, and create a folder named nodejs-helloworld. You can use any name.

> mkdir nodejs-helloworld
> cd nodejs-helloworld
> code .

“code .” is going to open the newly created folder in Visual Studio Code. You can open the folder even manually or with any other editor of your choice.

In the folder, create a new file named app.js

// app.js
console.log('Hello World..!!');

Now open up the terminal in the folder, and execute the command

> node app.js

It should log the string ‘Hello World..!!’ in the console.

Now, while this looks something, but nothing promising yet. We know that it’s a server side programming, so we should be able to start up a server. Right? So how do we do that?

Starting a Server with NodeJS

For creating a server with NodeJS, we need to use the inbuilt http package.

So let’s update the app.js to do something fancy 🙂

// app.js
const http = require('http');

const host = '127.0.0.1';
const port = 80;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.end('Hello World..!!');
});

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

Now execute, ‘node app.js’ in the terminal, the console should now print the log as

Server is running at http://127.0.0.1:80/
nodejs running server

If you notice the terminal, it hasn’t exited like the Hello World example. This means that the server is running, and is open for requests. So let’s visit the url printed in the console.

browser view

Viola! We have a server running on port 80, which is the default one. We can put any other port also.

To stop the server, hit Ctrl + C in the command line:

Note: Every time you make a change, you need to stop the server and start it again. Or, you can use nodemon npm package to listen for code changes and restart server when updates are made.

Some Important Node packages

We used one in-built node package http in our code earlier. There are thousand of packages in-built to node, and probably millions from 3rd-party developers worldwide. But a couple of more important ones are

const fs = require('fs');
const events = require('events');

Click here for node related articles.

Leave a Reply

Your email address will not be published. Required fields are marked *