Skip to main content

Command Palette

Search for a command to run...

Node.js Basics: What You Need to Know

Updated
4 min read

This is the very first blog in our nodejs series, in which i have no idea how long will it be, but one thing I know for sure is that by the end of it, we’ll have complete understanding about why node is the way it is.

I am having a lot of fun doing this, and I wish the same for you. I’ll keep this very hands on with lot of
code snippets and diagrams, I mean, of course, otherwise how is it different from that black n green
documentation, haha.

Prerequisites ( Good to have )

Though we’re starting from absolute basics, there are few concepts of Javascript that can this journey into
nodejs smoother.

  • Functions

  • Lexical structure

  • “this“ keyword

  • ES6 and beyond

  • asynchronous javascript (very important)

Javascript environment — Where node fits in

Lets quickly go through the Javascript’s history and purpose. Btw, do you how JS came into the picture,
in 1995, Brendan Eich created the first version of JS in just 10 days, its 2026 and people still wonder, how
is a language created in just 10 days rules the internet, and now, AI era as well. The ECMA specification came a year later when Netscape handed over the JavaScript language to ECMA International in order to set out a standard specification, which were then adopted by the browsers

Javascript was specifically created to work on the browsers, add interactivity to the static and boring websites. In very very simple words, we are interacting with the DOM(document object model), web api
like cookies etc

But, one thing is common in both the ecosystems, i.e. JS engine, which actually executes your JS code.

Just to mention a few, firefox uses spider monkey, safari uses JavascriptCore(nitro), chrome uses v8(our hero). The guy who built Nodejs had already worked on spider monkey, still he chose v8 for nodejs, why?

For performance? nah, v8 is open source. And this is how Ryan Dahl finds a space to bring Javascript code to the server.

Nodejs environment and setup

Installation

node -v
///
my version as of now 
24.1.0

If this command node -v giving you a version as shown above, great, you have node js installed on your machine. If not, you can simply go this url https://nodejs.org/en/download, here you can all the possible ways to install node. Pick any version with LTS (long term support).

First runnable server

As we are getting to the interesting stuff, best way to understand is getting the hands dirty ASAP, so here we go. Lets build a very simple http server with core nodejs module, nothing fancy just a simple sever running on localhost and returns a classic “hello world!“

// app.js
import http from 'http';
const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type':'text/plain'});
  res.end("hello world");
});
server.listen(3000, () => console.log('listening 3000'));

Lets see line by line whats happening here

  •   import http from “http“
    

    This imports the built-in http module from Node.js, which provides functionality to create HTTP servers and make HTTP requests.

  •   const server = http.createServer((req, res) => {
    

    Creates a new HTTP server instance. It takes a callback function (called the "request listener") that runs every time the server receives an incoming HTTP request. The callback receives two objects:

    • req (request): Contains information about the incoming request (URL, headers, method, etc.)

    • res (response): Used to send data back to the client

  •     res.writeHead(200, {'Content-Type':'text/plain'});
    

    Sets the HTTP status code and response headers before sending the body:

    • 200 is the HTTP status code for "OK" (success)

    • The second argument is an object with response headers

    • 'Content-Type':'text/plain' tells the browser the response is plain text, not HTML or JSON

  •     res.end('Hello world');
    

    sends the response body and ends the response, where hello world is the actual response that is sent to the client.

server.listen(3000, () => console.log('listening 3000'))

Starts the server and makes it listen for incoming connections on port 3000. The callback function runs when the server successfully starts, printing a confirmation message to the console.

How to run this code

  1. create a file named app.mjs in any code editor

  2. once, you save the file, run a command in the terminal node app.mjs

  3. visit http://localhost:3000 on any browser of your choice

  4. You’ll see Hello world returned on the screen.

This is the minimal "Hello World" HTTP server in Node.js!