Express.js – Build Your First Web Application
Express.js is a fast, minimal, and flexible Node.js framework used to build web applications and APIs easily. It simplifies server creation and routing compared to the basic HTTP module.
Client → Express Server → Response
1. What is Express.js?
Express.js is a backend framework built on top of Node.js that helps us:
- Create servers easily
- Handle routes
- Build APIs
- Manage requests and responses
Express.js removes complexity and makes Node.js development faster.
2. Installing Express.js
Run this command in your project folder:
npm init -y npm install express
3. Creating Your First Express Server
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Hello from Express.js");
});
app.listen(3000, () => {
console.log("Server running on port 3000");
});
Open browser: http://localhost:3000
4. Understanding the Code
- express() → Creates app
- app.get() → Handles route
- req → Request
- res → Response
- app.listen() → Starts server
5. Creating Multiple Routes
app.get("/", (req, res) => {
res.send("Home Page");
});
app.get("/about", (req, res) => {
res.send("About Page");
});
app.get("/contact", (req, res) => {
res.send("Contact Page");
});
6. Sending JSON Response
app.get("/api", (req, res) => {
res.json({ name: "Node.js", type: "Backend" });
});
7. Real-World Usage
- Building REST APIs
- Handling user requests
- Backend for web apps
- Microservices
8. Common Beginner Mistakes
- Forgetting to install express
- Not restarting server
- Wrong route path
Always restart server after changes.
Conclusion
Now you can build your first Express.js server easily.
Next: Express Routing (GET, POST, PUT, DELETE)
Your Node.js journey just became real backend development 🚀
Tags
NodeJS
