Working with Express.js Framework

📘 Node.js 👁 54 views 📅 Nov 05, 2025
⏱ Estimated reading time: 2 min

Express.js is a lightweight and flexible web framework built on top of Node.js.
It simplifies building web servers, APIs, and backend applications by providing a clean structure and powerful features.


1. Why Use Express.js?

Without Express:

  • Complex routing logic

  • Repetitive code for handling requests

  • Hard-to-maintain server setup

With Express:

  • Minimal code

  • Easy routing

  • Built-in middleware support

  • High performance


2. Installing Express

npm install express

3. Creating a Basic Express Server

const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Welcome to Express.js'); }); app.listen(3000, () => { console.log('Server running on port 3000'); });

4. Handling Routes

GET Request

app.get('/users', (req, res) => { res.send('User list'); });

POST Request

app.post('/users', (req, res) => { res.send('User created'); });

5. Request and Response Objects

Request (req)

  • req.params – URL parameters

  • req.query – Query strings

  • req.body – Request data

Response (res)

  • res.send() – Send response

  • res.json() – Send JSON

  • res.status() – Set HTTP status


6. Middleware in Express

Middleware functions execute between request and response.

Built-in Middleware

app.use(express.json());

Custom Middleware

app.use((req, res, next) => { console.log(req.method, req.url); next(); });

7. Serving Static Files

app.use(express.static('public'));

8. Route Parameters

app.get('/users/:id', (req, res) => { res.send(`User ID: ${req.params.id}`); });

9. Error Handling

app.use((err, req, res, next) => { res.status(500).send('Server Error'); });

10. Express Project Structure (Typical)

project/ │── routes/ │── controllers/ │── models/ │── app.js │── package.json

11. Advantages of Express.js

  • Fast and unopinionated

  • Easy to learn

  • Large ecosystem

  • Widely used


12. Express vs Node HTTP Module

Express.jsNode HTTP
Simple routingManual routing
Middleware supportNo middleware
Clean syntaxVerbose code

13. Summary

  • Express.js simplifies Node.js development

  • Provides routing and middleware

  • Ideal for REST APIs and web apps

  • Forms the base of many Node frameworks


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes