Nearby lessons

4 of 14

🛣️ Routing in Express JS

📌 What is Routing?

💡 What is Routing in Express.js?

Routing in Express.js defines how an application responds to client requests at specific endpoints (URLs) using HTTP methods such as GET, POST, and others.

Each route can have one or more handler functions that run when a route is matched, allowing Express apps to handle various request types efficiently.

🧱 Step 1: Basic Setup

To begin, create a new file named index.js and set up your basic Express app structure. All your routes will be defined inside this app.

Express.js Server
NODE RUNTIME
const express = require('express');
const cors = require('cors');
const app = express();

app.use(cors());

app.get('/', (req, res) => {
    res.send('<h1>Welcome to CodingsPoints</h1>');
});

app.listen(8080, () => {
    console.log('Server is running at http://localhost:8080');
});
Browser Output
Server logs

📁 Static Routes

Static routes are fixed URLs that always return the same content. These are commonly used for pages like Home, About, or Gallery.

Express.js Server
NODE RUNTIME
const express = require('express');
const cors = require('cors');
const app = express();

app.use(cors());

app.get('/', (req, res) => {
    res.send('<h1>Welcome to CodingsPoints</h1>');
});

app.get('/about', (req, res) => {
    res.send('<h1>Welcome to About Page</h1>');
});

app.get('/gallery', (req, res) => {
    res.send('<h1>Welcome to Gallery Page</h1>');
});

app.listen(8080, () => {
    console.log('Server is running at http://localhost:8080');
});
Browser Output
Server logs

📁 Nested and Specific Routes

Nested routes allow you to define subpaths under an existing route. You can also define specific literal paths (e.g., containing a dot).

Express.js Server
NODE RUNTIME
const express = require('express');
const app = express();

// Home page route
app.get('/', (req, res) => {
    res.send('<h1>Welcome to CodingsPoints</h1>');
});


// ------------------ Nested & Specific Routes ------------------

// Nested route under /about
app.get('/about/user', (req, res) => {
    res.send('<h1>Welcome to User Page</h1>');
});

// Specific route with dot (literal)
app.get('/random.text', (req, res) => {
    res.send('<h1>Welcome to random text</h1>');
});

app.listen(8080, () => {
    console.log('Server is running at http://localhost:8080');
});
Browser Output
Server logs

📁 Dynamic Route Parameters

Route parameters are dynamic values in the URL that represent specific resources, such as users or products. They are defined using a colon (:) followed by a name, and their values can be accessed using req.params.

Example URL: http://localhost:8080/user/10 → Output: { id: "10" }

Express.js Server
NODE RUNTIME
const express = require('express');
const app = express();

// Home page route
app.get('/', (req, res) => {
    res.send('<h1>Welcome to CodingsPoints</h1>');
});
// ------------------ Route Parameters ------------------

// Example: /user/101
app.get('/user/:id', (req, res) => {
    res.send(req.params); // { id: "101" }
});

app.listen(8080, () => {
    console.log('Server is running at http://localhost:8080');
});
Browser Output
Server logs

🔁 Multiple Parameters (Hyphen-Separated)

You can define multiple parameters within a single route using separators such as a hyphen (-). Express treats characters like hyphens as part of the URL path.

Example URL: http://localhost:8080/user/101-999 → Output: { uid: "101", bid: "999" }

Express.js Server
NODE RUNTIME
const express = require('express');
const app = express();

// Home page route
app.get('/', (req, res) => {
    res.send('<h1>Welcome to CodingsPoints</h1>');
});

// ------------------ Custom Pattern: Hyphen-Separated Params ----------------
// Example: /user/101-999
app.get('/user/:uid-:bid', (req, res) => {
    res.send(req.params); // { uid: "101", bid: "999" }
});

app.listen(8080, () => {
    console.log('Server is running at http://localhost:8080');
});
Browser Output
Server logs

🔍 Query Parameters

Query parameters are used to pass optional data such as filters, pagination, or search criteria.

They begin with a question mark (?) and are accessed via req.query.

Example URL: http://localhost:8080/search?name=rajesh&age=30&city=baroda

Express.js Server
NODE RUNTIME
const express = require('express');
const app = express();

// Home page route
app.get('/', (req, res) => {
    res.send('<h1>Welcome to CodingsPoints</h1>');
});


// ------------------ Query Parameters ------------------
// Example: /search?name=rajesh&age=30&city=baroda
app.get('/search', (req, res) => {
    const { name, age, city } = req.query;
    res.send(`Name: ${name}, Age: ${age}, City: ${city}`);
});

app.listen(8080, () => {
    console.log('Server is running at http://localhost:8080');
});
Browser Output
Server logs

🧠 Test Your Knowledge

5 Questions

Progress: 0 / 5
Keep Going!Render JSON & HTML