subdomain routing

2,337 views
Skip to first unread message

flippyhead

unread,
Dec 21, 2010, 1:24:54 PM12/21/10
to Express
How do I incorporate the subdomain as a part of routing?

e.g. I'd like to route one thing if a subdomain is present, another if
not

vision media [ Tj Holowaychuk ]

unread,
Dec 21, 2010, 3:07:38 PM12/21/10
to expre...@googlegroups.com
you could do a few things, maybe even the vhost middleware, but for example perhaps:

app.get('/foo', function(req, res, next){
  if (test for subdomain) return next();
  // no subdomain
});

app.get('/foo, ....);

you can re-define the same route, but it will only ever reach the second if the condition is met. Something that would be need is to implement this via route specific or regular middleware, if next() had support for next(n) where n was the number of middleware to skip ahead. I dunno, that makes things more complicated, but let me know if that is kinda what you are going for. There is nothing baked in though


--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.




--
Tj Holowaychuk
Vision Media
President & Creative Lead

Guillermo Rauch

unread,
Dec 21, 2010, 3:07:26 PM12/21/10
to expre...@googlegroups.com
There's many ways to do this (catchall route, middleware), but the basic premise is that you have to inspect the Host header of the request.

An example using a catchall route

app.get('*', function(req, res, next){
  if(req.headers.host == 'some.sub.domain.com')
    req.url = '/mysubdomain' + req.url;
  next();
});

This will mean that all get requests that come from the subdomain will get /subomdin appended to them, so then you can have routes like this

.get('/blogposts', function(){
  // for non-subdomain
})

.get('/mysubdomain/blogposts', function(){
   // for subdomain
})

If you want to do this for all requests (not just get), I suggest you write a middleware.

--
Guillermo Rauch
http://devthought.com

vision media [ Tj Holowaychuk ]

unread,
Dec 21, 2010, 3:12:04 PM12/21/10
to expre...@googlegroups.com
Good suggestion :) we recently added app.all() which allows you to map a function to all the HTTP methods for the given route, so just change Guillermo's app.get() to app.all() (if needed)

--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.

gnim

unread,
Jan 4, 2011, 7:03:50 AM1/4/11
to Express
Hi all,

I'm having problems with the 'app.all' method. It should be working,
so there is prob s.th wrong with my installation.
If anyone has a hint or idea, 't'd be most welcome. Details below.

Thanks,
Gnimm

-----------
$> node
> var express = require('express')
> var app = express.createServer();
> app.get
[Function]
> app.all
> app.all()
TypeError: Object [object Object] has no method 'all'
at [object Context]:1:5
at Interface.<anonymous> (repl:96:19)
at Interface.emit (events:27:15)
at Interface._ttyWrite (readline:309:12)
at Interface.write (readline:147:30)
at Stream.<anonymous> (repl:79:9)
at Stream.emit (events:27:15)
at IOWatcher.callback (net:489:16)
at node.js:773:9

---------------------------

$> npm ls express installed
npm info it worked if it ends with ok
npm info using n...@0.2.12-1
npm info using no...@v0.2.5
exp...@1.0.0rc4 Sinatra inspired web development framework
=tjholowaychuk installed remote framework sinatra web rest restful
exp...@1.0.0 Sinatra inspired web development framework
=tjholowaychuk installed remote framework sinatra web rest restful

$> npm ls connect installed
npm info it worked if it ends with ok
npm info using n...@0.2.12-1
npm info using no...@v0.2.5
con...@0.2.7 High performance middleware framework
=creationix =tjholowaychuk installed remote
con...@0.3.0 High performance middleware framework
=creationix =tjholowaychuk installed remote
con...@0.4.0 High performance middleware framework
=creationix =tjholowaychuk installed remote
con...@0.5.0 High performance middleware framework
=creationix =tjholowaychuk installed remote

gnim

unread,
Jan 4, 2011, 7:29:45 AM1/4/11
to Express
Funnily enough, using

app.routes.all

works fine (so far =)

//gnimm

vision media [ Tj Holowaychuk ]

unread,
Jan 4, 2011, 11:14:37 AM1/4/11
to expre...@googlegroups.com
hm yeah you should not need to use that. Definitely something funky going on with the install, maybe npm is exposing the wrong connect. you can use .version on express and connect at runtime as well to check. require('connect').version etc

--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.

gnim

unread,
Jan 6, 2011, 10:46:31 AM1/6/11
to Express
ah, good idea, I'll check the versions there.

Cheers again,
//gnimm

Todd

unread,
Apr 30, 2014, 10:51:45 PM4/30/14
to expre...@googlegroups.com
This is the best option in my humble. Very nice @Guillermo Rauch. HOWEVER, I do offer one suggestion if you're the copy-paste type: Most of us are likely using ports (i.e. :3000), so don't expect the above code to work unless you supply a port.

In this case, the code above would become:

app.get('*', function(req, res, next){
  if (req.headers.host == 'some.sub.domain.com:3000') {
    // any other node-sy logging and fun here
    req.url = '/mysubdomain' + req.url;
  }  
  next();
});

I offer this caveat simply because I've made this mistake myself, wasting a lot of time by overlooking this one simple tweak.

Awesome answer, @Guillermo.
Reply all
Reply to author
Forward
0 new messages