Setting Up Node.js and Your First Server
What is Node.js?
Node.js is a runtime environment that allows JavaScript to run outside of a browser. Before Node.js existed, JavaScript could only run in browsers -- it was purely a frontend language. Node.js changed that in 2009 by letting developers use JavaScript on the server.
This matters because:
- You can use one programming language (JavaScript) for both frontend and backend
- Node.js is fast, built on Google's V8 engine (the same engine that powers Chrome)
- It has a massive ecosystem of packages through npm (Node Package Manager)
- Companies like Netflix, LinkedIn, Uber, and PayPal use Node.js in production
The Event Loop: Why Node.js is Fast
Node.js uses an event loop and non-blocking I/O (Input/Output), which makes it very efficient for handling many requests at once.
Traditional servers create a new thread for every incoming request. With thousands of requests, this uses enormous amounts of memory.
Node.js takes a different approach: it uses a single thread and an event loop. Instead of waiting for a database query to finish, it starts the query and moves on to handle the next request. When the query finishes, Node.js returns to handle that response.
Think of it like a chef managing multiple dishes at once rather than cooking one dish at a time from start to finish.
Installing Node.js
To use Node.js, you need to install it on your computer:
- Go to nodejs.org
- Download the LTS (Long-Term Support) version
- Run the installer
Verify the installation by opening your terminal and running:
node --version
# Should output something like: v20.11.0
npm --version
# Should output something like: 10.2.4
Your First Node.js Script
Create a file called hello.js and write:
console.log("Hello from Node.js!");
const name = "Amara";
const greeting = `Welcome to backend development, ${name}!`;
console.log(greeting);
Run it with:
node hello.js
You just ran JavaScript on a server (your computer) instead of in a browser.
Building Your First HTTP Server
Node.js has a built-in module called http that lets you create a server without installing anything extra.
const http = require('http');
const server = http.createServer((request, response) => {
// Set the response status code and headers
response.writeHead(200, { 'Content-Type': 'text/plain' });
// Send the response body
response.end('Hello, World! Your backend server is working.');
});
// Tell the server to listen on port 3000
server.listen(3000, () => {
console.log('Server is running at http://localhost:3000');
});
Run this with node server.js and visit http://localhost:3000 in your browser. You will see the message.
Understanding the Code
Let us break down what each part does:
require('http'): Imports Node.js's built-in HTTP module. This is like an import statement.
http.createServer(callback): Creates a new server. The callback function runs every time a request comes in.
request: An object containing all information about the incoming request (URL, method, headers, body).
response: An object you use to build and send the server's reply.
response.writeHead(200, {...}): Sets the status code (200 means OK) and headers.
response.end('...'): Sends the response body and signals that the response is complete.
server.listen(3000, callback): Starts the server on port 3000.
What is a Port?
A port is a number that identifies a specific process or service running on a computer. Think of a building: the IP address is the building's street address, and the port is the door number.
Common ports:
- Port 80: HTTP (websites)
- Port 443: HTTPS (secure websites)
- Port 3000, 8000, 8080: Common development ports
During development, you use a port like 3000 on your own computer (localhost). When you deploy to production, your hosting service handles the ports.
npm and Packages
npm (Node Package Manager) is the world's largest software registry with over 2 million packages. Instead of writing everything from scratch, you install packages that other developers have built.
For example, instead of using Node's raw http module, most backend developers use Express.js -- a framework that makes building APIs much easier:
# Create a new project folder
mkdir my-backend
cd my-backend
# Initialise a Node.js project (creates package.json)
npm init -y
# Install Express.js
npm install express
This is the foundation of almost every Node.js backend project.
Try it yourself
Key Takeaways
- Node.js lets JavaScript run on the server, making it possible to use one language for both frontend and backend.
- Node.js uses a non-blocking event loop, which makes it efficient at handling many simultaneous requests.
- Every Node.js project starts with npm init, which creates a package.json to track dependencies.
- A server listens on a port number. Port 3000 is commonly used during development.
- Express.js is the most popular Node.js framework and makes building APIs significantly faster.
Quick Quiz
1.What is Node.js?
2.What does npm stand for?
3.What is a port in the context of backend servers?
4.Why is Node.js described as non-blocking?
Ready to go further?
CareerEx gives you structured 12-week training, live classes every Saturday and Sunday, real tutor feedback, and a certificate. Join the next cohort.
Join CareerEx