Why this route is working in express when not defined anywhere?

I'm new to express and stuck in a problem while following some tutorial. The problem is why this URL http://localhost:8000/user/profile is working? I haven't defined any route for /user/profile. If possible a deep explanation on this. How is this middleware working? router.use("/user", require("./users"));

This is main file (index.js)


    const express = require("express");
    const path = require("path");

    const port = 8000;
    const app = express();

    app.use("/", require("./routes/index"));

    app.listen(port, function(err) {
            console.log("Server started sucessfully at port", port);   
    });

This is routes file in different folder (index.js)


    const express = require("express");
    const router = express.Router();

    router.use("/user", require("./users"));
    router.get("/", function(req, res) {
        return res.end("<h1>Express is ready</h1>")
    });

    module.exports = router;

This is user file (user.js)


    const express = require("express");
    const router = express.Router();

    router.get("/profile", function(req, res) {
        res.end("<h1>User Controller</h1>");
    });

    module.exports = router;


In the main file you have defined this

app.use("/", require("./routes/index"));

it means express will use or any request that comes with '/' - will be redirected to route defined on route/index page

router.use("/user", require("./users"));

Similarly , in your route file you have defined /user - so any request that will come like /user , /user/xyz , /user/25 , (GET ,Post, put ,delete) will be handled/redirected to this user route.

and finally in your user file - (all /user request will come to this)

 router.get("/profile", function(req, res) {
        res.end("<h1>User Controller</h1>");
    });

You have written logic to handle /profile page.

So if you links all the steps - it's will be from root -> / -> /user -> /profile


Ok let's go from top to down. In your main index.js file you have this line

app.use("/", require("./routes/index")); What this does is every request coming to / endpoint is handled by ./routes/index file which you have given as a middleware

Now in routes index file you have similar function with /user endpoint router.use("/user", require("./users"));. Now every request coming to /user is handled by user.js file and there you have

router.get("/profile", function(req, res) {
        res.end("<h1>User Controller</h1>");
});

so now your endpoint becomes /user/profile.


Just to add to what was already said, when app.use() matches an incoming request url to the path at which it is mounted, it actually rewrites the url by removing the part of the path that matches.

You can see that with the following program:

var express = require('express')

var app = express()

app.use("/test", (req, _, next) => {
    console.log("original url path: " + req.originalUrl)
    console.log("current url path: " + req.url)
    next()
})

app.use("*", (_, res) => res.sendStatus(204))

app.listen(3000, () => console.log("Server listening."))

// console output when navigating to http://localhost:3000/test/whatever
original url path: /test/whatever
current url path:  /whatever       // the /test prefix was removed here.

If you pass a Router instance as a second argument to app.use(), the router will try to find matches for its own routes based on the current - rewritten - url.