Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Node.js: Express Framework and RESTful APIs

Lesson 41 of 46 in the free Web Technologies notes on Siksha Sarovar, written by Rohit Jangra.

Node.js: Express Framework and RESTful APIs

What is Express?

Express is a minimal, unopinionated web framework for Node.js that simplifies building servers, routing, and APIs on top of the core http module.

npm install express

A Basic Express Server

const express = require('express');
const app = express();

app.use(express.json()); // parse JSON request bodies

app.get('/', (req, res) => {
    res.send('Hello from Express!');
});

app.listen(3000, () => {
    console.log('Server running on http://localhost:3000');
});

Routing

Express maps HTTP methods and URL paths to handler functions.

app.get('/users', (req, res) => {
    res.json([{ id: 1, name: 'Alice' }]);
});

app.post('/users', (req, res) => {
    const newUser = req.body;
    res.status(201).json(newUser);
});

app.put('/users/:id', (req, res) => {
    res.json({ id: req.params.id, ...req.body });
});

app.delete('/users/:id', (req, res) => {
    res.status(204).send();
});

// Route parameters and query strings
app.get('/products/:category', (req, res) => {
    console.log(req.params.category);  // /products/electronics -> "electronics"
    console.log(req.query.sort);       // ?sort=price -> "price"
    res.send('OK');
});

Middleware

Middleware functions run between the request and the final handler — used for logging, authentication, parsing, error handling.

// Custom middleware: runs for every request
app.use((req, res, next) => {
    console.log(`${req.method} ${req.url}`);
    next(); // pass control to the next middleware/handler
});

// Middleware scoped to a specific route
function authenticate(req, res, next) {
    if (req.headers.authorization) {
        next();
    } else {
        res.status(401).json({ error: 'Unauthorized' });
    }
}

app.get('/admin', authenticate, (req, res) => {
    res.send('Welcome, admin!');
});

Building a RESTful API

REST (Representational State Transfer) is an architectural style where each URL represents a resource, and HTTP methods define the action on it.

HTTP MethodURLAction
GET/api/booksList all books
GET/api/books/:idGet one book
POST/api/booksCreate a book
PUT/api/books/:idUpdate a book
DELETE/api/books/:idDelete a book
const express = require('express');
const app = express();
app.use(express.json());

let books = [{ id: 1, title: 'Clean Code' }];

app.get('/api/books', (req, res) => res.json(books));

app.get('/api/books/:id', (req, res) => {
    const book = books.find(b => b.id == req.params.id);
    if (!book) return res.status(404).json({ error: 'Not found' });
    res.json(book);
});

app.post('/api/books', (req, res) => {
    const book = { id: books.length + 1, ...req.body };
    books.push(book);
    res.status(201).json(book);
});

app.put('/api/books/:id', (req, res) => {
    const index = books.findIndex(b => b.id == req.params.id);
    if (index === -1) return res.status(404).json({ error: 'Not found' });
    books[index] = { ...books[index], ...req.body };
    res.json(books[index]);
});

app.delete('/api/books/:id', (req, res) => {
    books = books.filter(b => b.id != req.params.id);
    res.status(204).send();
});

app.listen(3000, () => console.log('REST API running on port 3000'));

REST Best Practices

  1. Use nouns, not verbs, in URLs (/books, not /getBooks)
  2. Use proper HTTP status codes (200, 201, 400, 404, 500)
  3. Version your API (/api/v1/books)
  4. Return JSON consistently
  5. Use plural resource names
Key Takeaway: Express simplifies routing and middleware on top of Node's http module. RESTful APIs map HTTP methods (GET/POST/PUT/DELETE) to CRUD operations on named resources, returning JSON with proper status codes.