What is Node.js? Node.js is an open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside a web browser. It allows developers to build scalable network applications quickly and efficiently. Think of it as JavaScript on the server-side!

Prerequisites Before we begin, make sure you have a basic understanding of JavaScript. You don't need to be an expert, but familiarity with variables, functions, and control flow will be helpful.
Download and Install Node.js The first step is to get Node.js installed on your system. Visit the official Node.js website and download the recommended LTS (Long Term Support) version for your operating system.
- Visit the official website: Go to nodejs.org.

- Download the installer: Choose the installer for your operating system (Windows, macOS, Linux).

- Run the installer: Follow the prompts to complete the installation. It's usually a 'Next, Next, Finish' process.
- Verify installation: Open your terminal or command prompt and type
node -v
andnpm -v
. You should see the installed versions, confirming Node.js and npm (Node Package Manager) are ready.

Project Setup: Create Your Folder and Open in VS Code
First, create a new folder for your project, for example, my-first-server
.

Now, open this folder in Visual Studio Code. A quick way to do this is to click on the address bar in your file explorer, type cmd
, and press Enter. This will open a command prompt in that directory. Then, type code .
and press Enter. This command will open the current folder in Visual Studio Code.


Minimal Server Code Inside your my-first-server
folder, create a new file named server.js
. This will be where our server code lives. Now, let's write the code for our basic HTTP server. Paste the following code into your server.js
file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const http = require('http');
const hostname = '127.0.0.1';
const port = 8080;
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}/`);
});

Code Explanation:
const http = require('http');
: This line imports Node.js's built-inhttp
module, which provides functionality for creating HTTP servers.const hostname = '127.0.0.1';
: This sets the IP address where our server will listen. '127.0.0.1' is the localhost.const port = 3000;
: This sets the port number on which our server will listen for incoming requests.const server = http.createServer((req, res) => { ... });
: This creates an HTTP server object. The callback function(req, res)
is executed every time a request is made to our server.req
(request) object: Contains information about the incoming request (e.g., URL, headers).res
(response) object: Used to send data back to the client.
res.statusCode = 200;
: Sets the HTTP status code to 200, indicating a successful response.res.setHeader('Content-Type', 'text/plain');
: Sets the content type of the response. Here, we're sending plain text.res.end('Hello World!\n');
: Sends the 'Hello World!' text as the response body and closes the connection.server.listen(port, hostname, () => { ... });
: Starts the server, making it listen for connections on the specified port and hostname. The callback function is executed once the server starts successfully.
Run Command To run your server, open the integrated terminal in VS Code (Ctrl+J or Terminal > New Terminal) or your system's terminal/command prompt. Navigate to your my-first-server
directory if you're not already there, and execute the following command:
1
node server.js
You should see the message Server running at http://127.0.0.1:8080/
in your terminal. This means your server is now active and listening for requests!

Now, open your web browser and go to http://127.0.0.1:8080/
. You should see 'Hello World!' displayed in your browser. Congratulations, you've successfully created and run your first Node.js server!

What is Express.js? Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. It simplifies the process of building server-side applications with Node.js, offering tools for routing, middleware, and more.

Install Express.js To use Express.js in your project, you need to install it using npm. Open your terminal in your project directory (my-first-server
) and run the following command:
1
npm install express

Sample Express Code Create a new file named express-app.js
in your project directory and paste the following minimal Express.js server code:
1
2
3
4
5
6
7
8
9
10
11
const express = require('express');
const app = express();
const port = 8080;
app.get('/', (req, res) => {
res.send('Hello World!')
});
app.listen(port, () => {
console.log(`Server running at http://${hostname}:${port}/`)
});

Command to Run To run your Express.js application, execute the following command in your terminal:
1
node server.js
You should see the message Express app listening at http://localhost:8080
in your terminal. This means your Express.js server is now active!

URL to Open in Browser Open your web browser and navigate to the following URL:
http://localhost:8080

Final Thoughts: This is just the beginning! Node.js is incredibly powerful for building all kinds of network applications, from simple APIs to real-time chat applications. Keep exploring the official documentation and building more complex projects.
About the Author: Zeeshan Ali is a passionate developer with expertise in frontend, backend, AI, and blockchain. Explore my open-source projects and tutorials on GitHub: Zeeshan Ali's GitHub Profile. You can also connect with me on LinkedIn: Zeeshan Ali's LinkedIn Profile.
