About the problem of express routin

var router = express.Router();

router.get("/", function(req, res) {
  res.send("<h1>Hello World</h1>");
});

app.use("/home", router)
The

above code creates a new routing object that returns Hello World when the access root route (/) is specified. The route is then loaded in the / home path, that is, accessing / home returns Hello World.
but if you add a route

 router.get("/a", function(req, res) {
      res.send("<h1>Hello pojia</h1>");
    });

there are two routes"/","/ a"at this time. What will be output if you visit"/ home" at this time? Why?

Apr.02,2021

/ home output Hello World
/ home/a will output Hello pojia
/ and / an are both Notfound


Hello World


you happen to use express's skill of managing routing levels.
app.use ('/ home',router)-express transfers requests under route / home to router. / and / a set in router are relative to / home , that is, they correspond to / home/ and / home/a respectively.
above, the "hierarchical" management of routing is realized.

Menu