skip to Main Content

I am new in express.js and I want to know how to use "path" in app.use() excatly. When I try the code which is following, I get a result but I am so confusing. Can you explain why the result is like that. Thank you so much.

My code is following:

const express = require('express');
const router = express.Router()
const app  = express();
const port = 3000;

app.listen(port, ()=>{
    console.log("Running");
})

app.use("/test",router)
// app.use()


router.get("/test",(req,res,next)=>{
    console.log("test 1");
    console.log(req.originalUrl);
    next()
})


router.get("/",(req,res,next)=>{
    console.log("first /");
    console.log(req.originalUrl);
    next()
})
router.get("/test",(req,res,next)=>{
    console.log("test 2");
    console.log(req.originalUrl);
    next()
})

router.get("/",(req,res,next)=>{
    console.log("second /");
    console.log(req.originalUrl);
    next()
})

And, I did test in "http://localhost:3000/test" but the result is not as I expected. Console output is :

first /
/test
second /
/test

But I was expected that result will be

test 1
/test
test 2
/test

Why is it like that? Could you explain to me? Thank you so much.

2

Answers


  1. const express = require('express');
    const router = express.Router();
    const app = express();
    const port = 3000;
    
    
    router.get("/", (req, res, next) => {
        console.log("test 1");
        console.log(req.originalUrl);
        next(); 
    });
    
    router.get("/", (req, res, next) => {
        console.log("test 2");
        console.log(req.originalUrl);
    
    });
    
    app.use("/test", router);
    
    app.listen(port, () => {
        console.log("Running on port", port);
    });
    

    This discrepancy occurs because router.get("/test",...) is actually looking for a path of /test/test due to the way routers are nested and paths are interpreted in Express.js. Since such a path is not defined in your setup, the request to /test is handled by the routes mounted at /test and defined as / within the router, not the ones looking for /test within the router.

    Login or Signup to reply.
  2. The output you’re seeing is correct based on the current implementation.

    Currently you have the following top level routes defined in your instance:

    localhost:3000/test
    localhost: 3000/test/test

    Under the /test route you’ve setup two functions / middleware:

    #1 – first /
    #2 – second /

    Under the /test/test route you currently have another two functions / middleware setup:

    #1 – test 1
    #2 – test 2

    Perhaps restructuring the code (shown below) can help visualize the order in which the request will be handled based on the route:

    router.get("/test",(req,res,next)=>{
        console.log("test 1");
        console.log(req.originalUrl);
        next()
    })
    
    router.get("/test",(req,res,next)=>{
        console.log("test 2");
        console.log(req.originalUrl);
        next()
    })
    
    router.get("/",(req,res,next)=>{
        console.log("first /");
        console.log(req.originalUrl);
        next()
    })
    
    router.get("/",(req,res,next)=>{
        console.log("second /");
        console.log(req.originalUrl);
        next()
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search