🚀 Setting Up a Basic Express Server

Hi, I'm Vishal Gupta, a passionate and self-driven Full-Stack Developer with a strong command of the MERN stack (MongoDB, Express.js, React.js, Node.js). I love turning complex problems into clean, scalable solutions through elegant code and user-friendly interfaces. With hands-on experience building real-world applications like MedEase, TripGenius, and now this Expense Tracker, I strive to build software that’s not only functional but also impactful. Whether it's creating secure authentication systems, responsive UI components, or integrating AI-powered features, I aim to deliver value with every line of code. 🔧 Key Skills: Frontend: React.js, Vite, Tailwind CSS, ShadCN, Context API Backend: Node.js, Express.js, MongoDB, JWT, REST APIs Dev Tools: Git & GitHub, Postman, Firebase, Vercel Soft Skills: Problem-solving, communication, and a strong desire to learn & grow I believe in continuous learning and building meaningful products that solve real-life problems. Currently, I’m open to opportunities where I can collaborate, innovate, and contribute to exciting web development projects.
If you're starting out with backend development in Node.js, Express is the first framework you'll likely touch — and for good reason. It’s lightweight, unopinionated, and lets you get a server running in minutes. This guide walks you through setting up a basic Express server from scratch, with clear explanations and copy-ready code snippets.
📌 What is Express?
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for building APIs and backend applications.
In simple words:
➡️ It lets you create an HTTP server quickly without dealing with Node’s low-level http module.
🛠️ Step 1 — Initialize Your Project
First, create a folder and initialize a Node project:
mkdir my-express-server
cd my-express-server
npm init -y
This creates a package.json file that keeps track of your project dependencies.
📦 Step 2 — Install Express
Run:
npm install express
That’s it — you now have Express in your project.
📁 Step 3 — Create Your Server File
Create a file named:
server.js
or
index.js
Inside it, write:
const express = require("express");
const app = express();
// Middleware to parse JSON
app.use(express.json());
// Basic route
app.get("/", (req, res) => {
res.send("Hello from Express!");
});
// Start the server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
🧩 Understanding the Code
1. Importing Express
const express = require("express");
This loads the Express module.
2. Creating the App
const app = express();
app is the main server instance. You’ll use it to define routes, middleware, etc.
3. Middleware
app.use(express.json());
This allows Express to read JSON from request bodies (req.body).
4. Routes
app.get("/", (req, res) => {
res.send("Hello from Express!");
});
A simple GET endpoint at /.
5. Starting the Server
app.listen(PORT, () => { ... });
Tells Express to listen for incoming requests.
🧪 Step 4 — Run the Server
In your terminal:
node server.js
Output:
Server running on http://localhost:5000
Open your browser → http://localhost:5000
You should see:
Hello from Express!
📌 Common Enhancements (Optional but Useful)
🔥 Add Nodemon for Auto-Restart
npm install --save-dev nodemon
Update package.json:
"scripts": {
"dev": "nodemon server.js"
}
Run:
npm run dev
Now your server reloads automatically whenever you edit code.
📡 Adding Another Route
app.post("/api/data", (req, res) => {
const body = req.body;
res.json({
message: "Data received successfully",
received: body,
});
});
❗ Adding 404 Not Found Handler
app.use((req, res) => {
res.status(404).json({ error: "Route not found" });
});
This article provides a step-by-step guide to setting up a basic Express server in Node.js. You'll learn how to initialize a project, install Express, and create a server file with routes and middleware. Additionally, it covers running your server and useful enhancements like using Nodemon for auto-restarts and handling 404 errors. Perfect for beginners looking to quickly get a server up and running.
Thank you for reading this till the end.
If you see any error,
please let me know by commenting or write to me on this email - abhimanyug987@gmail.com



