Where i lost my URL in express js

I was trying to build an app with different subdomains . I can fetch each subdomain by subdomain module and can access like /subdomain/user . I'm getting two different url in my modules even it shares same callback handle for listening the server

Lets look at code briefly

main.js

var subdomain=require('subdomain')({ base: 'example.loc' });
var express=require('express')
var user=require('./app/user');

express()
.use(subdomain)
.use(function  (req,res,next) {
    console.log('This is from main');
    console.log(req.url);
    next();
})
.use('/subdomain/user',user)
.listen(8808)

user.js

var app = require('express')();
var routes=require('./routes');
    app
    .use(routes);

module.exports=app

routes/index.js

var express=require('express');
var router=express.Router();

router.use(function  (req,res,next) {
    console.log('This from user');
    console.log(req.url);
    next();
})
router.get('/subdomain/user',function(req,res,next){
 res.send('This is from user');
});
module.exports = router;

If i call the url , user.example.loc im getting log as follows

This is  from  main
/subdomain/user
This is from  user
/

Question is , where i lost the url i assigned using subdomain module . I checked the subdomain module source , i could not find any reason for this issue.

An express app is actually a Router and Router.use() strips the mount path from the url before processing it.

From the Router docs:

Every express application has a builtin app router.

And from the Router.use() docs:

The "mount" path is stripped and is not visible to the middleware function. The main effect of this feature is that mounted middleware may operate without code changes regardless of its "prefix" pathname.

So, you're function here:

router.get('/subdomain/user',function(req,res,next){
    res.send('This is from user');
});

isn't getting called because the router it is a part of is mounted to /subdomain/user and the gets stripped away (as you've seen). If you change it's path to / it should work:

router.get('/',function(req,res,next){
    res.send('This is from user');
});