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:

Javascript




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


Start using the following command:

node server.js

Output:

Output

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.

Similar Reads

Routing in Express:

Routing in Express is like showing your web app where to go. It’s about deciding what your app should do when users go to various URLs. You get to set the actions for things like going to the homepage, submitting forms, or clicking links. Express makes it simple by letting you create rules that connect to specific parts of your code. In essence, it’s like telling your app, “Hey, when someone goes here, do this specific thing.”...

Steps to Implement Routing in Express:

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

Advantages of Routing in Express:

...