Express.js is the most popular web framework for Node.js. It makes building web applications, REST APIs, dashboards, and backend services much easier than using Node.js alone. If you're learning backend development, Express.js is one of the first frameworks you should master.
Before starting, make sure Node.js and npm are installed on your computer. If not, read our guides: How to Install Node.js on Windows, Linux, and macOS and What Is NPM? Complete Guide for Beginners.
What Is Express.js?
Express.js is a lightweight and flexible web application framework for Node.js. It simplifies tasks such as routing, middleware management, request handling, response generation, and API development.
Why Developers Love Express.js
- Simple and easy to learn
- Fast and lightweight
- Perfect for REST APIs
- Large ecosystem and community
- Works well with MongoDB, MySQL, PostgreSQL
- Ideal for scalable applications
Create a New Project
Create a new directory:
mkdir express-tutorial
cd express-tutorial
Initialize npm:
npm init -y
This creates a package.json file for your project.
Install Express.js
Install Express.js using npm:
npm install express
After installation, Express.js will appear in your project's dependencies.
Build Your First Web Server
Create a file named:
server.js
Add the following code:
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
res.send('Hello World from Express.js!');
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
Run the Server
node server.js
Open your browser and visit:
http://localhost:3000
You should see:
Hello World from Express.js!
Understanding Routes
Routes determine how your application responds to client requests.
app.get('/about', (req, res) => {
res.send('About Page');
});
app.get('/contact', (req, res) => {
res.send('Contact Page');
});
Now these URLs will work:
- /about
- /contact
Handling Route Parameters
Dynamic routes allow you to capture URL values.
app.get('/user/:id', (req, res) => {
res.send(`User ID: ${req.params.id}`);
});
Example:
/user/25
Output:
User ID: 25
Handling Query Parameters
app.get('/search', (req, res) => {
res.send(`Search Term: ${req.query.q}`);
});
Example:
/search?q=nodejs
What Is Middleware?
Middleware functions execute before requests reach route handlers.
app.use((req, res, next) => {
console.log('Request Received');
next();
});
Middleware is commonly used for:
- Authentication
- Logging
- Validation
- Security
- Error Handling
Handling JSON Requests
Enable JSON parsing:
app.use(express.json());
Create a POST endpoint:
app.post('/users', (req, res) => {
res.json({
message: 'User created',
data: req.body
});
});
Creating a Simple REST API
app.get('/api/products', (req, res) => {
res.json([
{ id: 1, name: 'Laptop' },
{ id: 2, name: 'Mouse' }
]);
});
Visiting the endpoint returns JSON data.
Serving Static Files
Create a folder:
public
Add:
app.use(express.static('public'));
Express.js will automatically serve files from the public folder.
Error Handling
app.use((err, req, res, next) => {
res.status(500).json({
error: err.message
});
});
Proper error handling improves application reliability and debugging.
Recommended Project Structure
project/
│
├── server.js
├── package.json
├── routes/
├── controllers/
├── models/
├── middleware/
├── public/
└── views/
As your application grows, organizing files properly becomes increasingly important.
Best Practices
- Keep routes modular
- Use environment variables
- Implement proper error handling
- Validate user input
- Use middleware effectively
- Follow REST API conventions
- Use version control (Git)
What to Learn Next
- Express.js Routing
- Middleware Deep Dive
- MongoDB with Mongoose
- MySQL with Node.js
- Authentication & JWT
- REST API Development
- Express.js Security Best Practices
- Deploying Express.js Applications
Conclusion
Express.js is one of the easiest and most powerful frameworks for building web applications with Node.js. By learning routing, middleware, request handling, and API development, you'll have the foundation needed to create professional backend applications.
Whether you're building a blog, e-commerce platform, HRMS, ERP system, REST API, or enterprise application, Express.js provides a solid foundation for scalable web development.
About ShasTech-IT
ShasTech-IT develops modern web applications, Node.js solutions, Android apps, ERP systems, HRMS platforms, WMS software, POS systems, and custom business solutions tailored to client requirements.