Open In App

What is routing in Express?

Express routing is about showing your app how to respond to different URLs. It involves associating HTTP methods with specific functions to handle requests. This helps organize and control the flow of your web application.

Routing in Express:

Steps to Implement Routing in Express:

Step 1: Initialising the Node App using the below command:



npm init -y

Step 2: Installing express in the app:

npm install express

Example: Below is the example of routing in express:






// server.js
const express = require('express');
const app = express();
const PORT = 4000;
 
// define route
app.get('/home',
    (req, res) => {
        res.send(
            '<h1>welcome to GeeksforGeeks!</h1>'
        );
    });
 
app.listen(PORT,
    () => {
        console.log(
            `Server is listening at http://localhost:${PORT}`
        );
    });

Start using the following command:

node server.js

Output:

Output

Advantages of Routing in Express:

Article Tags :