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.