Skip to content

Commit

Permalink
docs: add more information on the handle argument
Browse files Browse the repository at this point in the history
closes #12
  • Loading branch information
dougwilson committed Jul 20, 2015
1 parent 795ec8c commit fb8874a
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ $ npm install vhost
var vhost = require('vhost')
```

### vhost(hostname, server)
### vhost(hostname, handle)

Create a new middleware function to hand off request to `server` when the incoming
host for the request matches `hostname`.
Create a new middleware function to hand off request to `handle` when the incoming
host for the request matches `hostname`. The function is called as
`handle(req, res, next)`, like a standard middleware.

`hostname` can be a string or a RegExp object. When `hostname` is a string it can
contain `*` to match 1 or more characters in that section of the hostname. When
Expand Down Expand Up @@ -112,6 +113,37 @@ app.use(vhost('*.userpages.local', userapp))
app.listen(3000)
```

### using with any generic request handler

```js
var connect = require('connect')
var http = require('http')
var vhost = require('vhost')

// create main app
var app = connect()

app.use(vhost('mail.example.com', function (req, res) {
// handle req + res belonging to mail.example.com
res.setHeader('Content-Type', 'text/plain')
res.end('hello from mail!')
}))

// an external api server in any framework
var httpServer = http.createServer(function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello from the api!')
})

app.use(vhost('api.example.com', function (req, res) {
// handle req + res belonging to api.example.com
// pass the request to a standard Node.js HTTP server
httpServer.emit('request', req, res)
}))

app.listen(3000)
```

## License

[MIT](LICENSE)
Expand Down

0 comments on commit fb8874a

Please sign in to comment.