3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Build Status][travis-image]][travis-url]
6 [![Test Coverage][coveralls-image]][coveralls-url]
7 [![Gratipay][gratipay-image]][gratipay-url]
18 var vhost = require('vhost')
21 ### vhost(hostname, handle)
23 Create a new middleware function to hand off request to `handle` when the incoming
24 host for the request matches `hostname`. The function is called as
25 `handle(req, res, next)`, like a standard middleware.
27 `hostname` can be a string or a RegExp object. When `hostname` is a string it can
28 contain `*` to match 1 or more characters in that section of the hostname. When
29 `hostname` is a RegExp, it will be forced to case-insensitive (since hostnames are)
30 and will be forced to match based on the start and end of the hostname.
32 When host is matched and the request is sent down to a vhost handler, the `req.vhost`
33 property will be populated with an object. This object will have numeric properties
34 corresponding to each wildcard (or capture group if RegExp object provided) and the
35 `hostname` that was matched.
38 // for match of "foo.bar.example.com:8080" against "*.*.example.com":
39 req.vhost.host === 'foo.bar.example.com:8080'
40 req.vhost.hostname === 'foo.bar.example.com'
41 req.vhost.length === 2
42 req.vhost[0] === 'foo'
43 req.vhost[1] === 'bar'
48 ### using with connect for static serving
51 var connect = require('connect')
52 var serveStatic = require('serve-static')
53 var vhost = require('vhost')
55 var mailapp = connect()
57 // add middlewares to mailapp for mail.example.com
59 // create app to serve static files on subdomain
60 var staticapp = connect()
61 staticapp.use(serveStatic('public'))
66 // add vhost routing to main app for mail
67 app.use(vhost('mail.example.com', mailapp))
69 // route static assets for "assets-*" subdomain to get
70 // around max host connections limit on browsers
71 app.use(vhost('assets-*.example.com', staticapp))
73 // add middlewares and main usage to app
78 ### using with connect for user subdomains
81 var connect = require('connect')
82 var serveStatic = require('serve-static')
83 var vhost = require('vhost')
85 var mainapp = connect()
87 // add middlewares to mainapp for the main web site
89 // create app that will server user content from public/{username}/
90 var userapp = connect()
92 userapp.use(function(req, res, next){
93 var username = req.vhost[0] // username is the "*"
95 // pretend request was for /{username}/* for file serving
96 req.originalUrl = req.url
97 req.url = '/' + username + req.url
101 userapp.use(serveStatic('public'))
106 // add vhost routing for main app
107 app.use(vhost('userpages.local', mainapp))
108 app.use(vhost('www.userpages.local', mainapp))
110 // listen on all subdomains for user pages
111 app.use(vhost('*.userpages.local', userapp))
116 ### using with any generic request handler
119 var connect = require('connect')
120 var http = require('http')
121 var vhost = require('vhost')
126 app.use(vhost('mail.example.com', function (req, res) {
127 // handle req + res belonging to mail.example.com
128 res.setHeader('Content-Type', 'text/plain')
129 res.end('hello from mail!')
132 // an external api server in any framework
133 var httpServer = http.createServer(function (req, res) {
134 res.setHeader('Content-Type', 'text/plain')
135 res.end('hello from the api!')
138 app.use(vhost('api.example.com', function (req, res) {
139 // handle req + res belonging to api.example.com
140 // pass the request to a standard Node.js HTTP server
141 httpServer.emit('request', req, res)
151 [npm-image]: https://img.shields.io/npm/v/vhost.svg
152 [npm-url]: https://npmjs.org/package/vhost
153 [travis-image]: https://img.shields.io/travis/expressjs/vhost/master.svg
154 [travis-url]: https://travis-ci.org/expressjs/vhost
155 [coveralls-image]: https://img.shields.io/coveralls/expressjs/vhost/master.svg
156 [coveralls-url]: https://coveralls.io/r/expressjs/vhost
157 [downloads-image]: https://img.shields.io/npm/dm/vhost.svg
158 [downloads-url]: https://npmjs.org/package/vhost
159 [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg
160 [gratipay-url]: https://gratipay.com/dougwilson/