Routing

Express JS Basic Routing is done using basic http methods like GET, POST and so on. We have done basic routing in last article using GET and POST.

See Examples of basic routing

Express JS GET


import express from 'express';
const app=express();

app.get('/',(req,res)={
    console.log("get method);
    res.send('get');
});   
    

Express JS POST


import express from 'express';
const app=express();

app.post('/',(req,res)={
    console.log("post method);
    res.send('post');
});   
    

Express Router

Express JS use express.Router class to create modular and mountable route handlers. The Router instance also known as Mini-app, is a complete middleware routing system in ExpressJS.


import express from 'express';
const router=express.Router();


Router.get

Now we are using Router.use to add a middleware function. We will create two modules, one for admin and second for user.

Create admin route

    /* admin.js */

import express from 'express';
const admin=express.Router();

admin.get('/',(req,res)=>{
    console.log("hi admin");
    res.status(200).send("hi admin");
});

export default admin;

Run route in main app

    /* app.js */

import express from 'express';
const app=express();
import admin from './admin.js';


app.use('/admin',admin);

app.listen(3000)

hi admin

node src/app.js

Router Example

In the example below, we are using two different modules , admin and user.

Admin route

    /* admin.js */

import express from 'express';
const admin=express.Router();

// middleware specific to admin
admin.use(function timeLog (req, res, next) {
    console.log('Admin login at: ', Date.now())
    next()
});

admin.get('/',(req,res)=>{
    console.log("hi admin");
    res.status(200).send("hi admin");
});

export default admin;

User route

    /* user.js */

import express from 'express';
const user=express.Router();

// middleware specific to user
user.use(function timeLog (req, res, next) {
    console.log('User login at: ', Date.now())
    next()
});

user.get('/',(req,res)=>{
    res.status(200).send("hi user");
});


export default user;

Main App

    /* app.js */
import express from 'express';
const app=express();

import admin from './admin.js';
import user from './user.js';


app.use('/admin',admin);
app.use('/user',user);

app.listen(3000,()=>{ console.log('server running')})

Run App

server running

node src/app.js

Now lets open both admin and user pages one by one to see routing. Open a web browser and type following URLs one by one.

Admin login at: 1589455931358

127.0.0.1:3000/admin

User login at: 1589455932090

127.0.0.1:3000/user

Real World Example

Lets create a courses router with various courses. We will create a custom middleware for handling course-related requests. User can access different course endpoints through this router. We can also add custom 404 handling for specific course routes.


    import express from 'express';
    const courses=express.Router();

    courses.get("/",(req,res)=>{
        res.status(200).send("All Courses");
    });

    courses.get("/nodejs",(req,res)=>{
        res.status(200).send("Node JS Course");
    });

    courses.get("/react",(req,res)=>{
        res.status(200).send("React Course");
    });

    courses.use((req,res)=>{
        res.status(404).send("Course Not Found");
    });

    export default courses;

In this example, we have created a simple courses router with endpoints for different courses and a 404 handler for undefined routes.