Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master

Merge pull request #2 from elsbree/master

Allow Dashes in Subdomains
latest commit e1acfb32d9
Wilson Page authored
Failed to load latest commit information.
lib Got rid of the newline at the end of the file.
.gitignore - Fixed an error Update
index.js First commit
package.json - Remove unwanted dir


express-subdomain-handler takes the headache out of dynamic subdomain routing in Express. It captures the contents of any subdomain and writes them into the Express req.url. This means you can write specific route handlers for subdomain urls. As you can see below, express-subdomain-handler can manage single or multiple subdomains.

Examples => '/subdomain/mysubdomain/' => '/subdomain/myexcellentsubdom/homepage' => '/subdomain/first/second/' => '/subdomain/first/second/another/page'


npm install express-subdomain-handler


Add express-subdomain-handler to your express middleware stack (before your routes are specified). You need to specify what the base url of your site is ('', 'example.local', etc), what you what subdomain urls to be prefixed with ('subdomain' by default) and whether you want logging turned on (false by default)

app.use( require('express-subdomain-handler')({ baseUrl: '', prefix: 'myprefix', logger: true }) );  

Setup routes to catch subdomain urls so for I would write my route handler to look like this.

app.get('/myprefix/:thesubdomain/thepage', function(req, res, next){

    // for the example url this will print 'mysubdomain'


Follow me

If you like my stuff, follow me on Twitter

Something went wrong with that request. Please try again.