Sub domains on ExpressJS

Problem

I am developing an application with ExpressJS. And i want to use sub domains on this application.(Virtual subdomains)

Is there any way to interprete this url;

http://customer_name.mysite.com

as

http://mysite.com/customer_name

My current url mapping on cntroller like this;

app.get('/:customer_name/?', function(req, res, next) {
//something functions
});

Thanks in advance

Problem courtesy of: Hüseyin BABAL

Solution

I have added wildcard domain on my domain provider(GoDaddy). You can refer here for how to add wildcard domain

And then, i have prepared a middleware that parses url and get subdomain part. Query subdomain text as user that's all.

For getting subdomain, you can refer here. Simply get host name and split it. First one is the subdomain name.

Solution courtesy of: Hüseyin BABAL

Discussion

Nodejs has nothing to do with these stuff. That's what the server is meant to handle with. Node just listens to a port and that's it. It's your job to forward any host to that port. So, I guess you should play with your server setup.

Discussion courtesy of: Krasimir

That's exactly the usecase of the express-subdomains module.

Your require it, tell it the subdomains and all requests on these subdomains are available as prefixes to your routes.

For example

customer_name.mysite.com/route

would be mapped to

mysite.com/customer_name/route

Discussion courtesy of: sbugert

This recipe can be found in it's original form on Stack Over Flow.