Aman Gupta
Aman Gupta

Reputation: 3797

ExpressJS: Subdomain

In my app, I am creating multiple users like user1,user2... and want to assign subdomain for each user like user1.xyz.com,user2.xyz.com ... Following are the requirements for the app:

1) Create these subdomains dynamically as the user is registered with the app

2) GET user1.xyz.com should call a route like /user1/home

I have seen a npm module https://www.npmjs.com/package/express-subdomain but looks like it will work only for static subdomains.

Upvotes: 0

Views: 721

Answers (1)

Aman Gupta
Aman Gupta

Reputation: 3797

Found a package which fulfills the requirement:

https://github.com/edwardhotchkiss/subdomain.

app.use(subdomain({ base : 'localhost', removeWWW : true }));

app.get('/subdomain/:name', Controller});    //GETs user1.localhost and user1 can be used as parameter using req.params.name

Eg.

app.get('/subdomain/:name',function(req,res){
  var name = req.params.name;
  res.send(name);
});

Upvotes: 1

Related Questions