Skip to content

Add support for reverse proxies by checking req.hostname or req.host. Fixes #20 #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ jobs:
- name: Node.js 13.x
node-version: "13.14"

- name: Node.js 13.x
- name: Node.js 14.x
node-version: "14.21"

- name: Node.js 15.x
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ property will be populated with an object. This object will have numeric propert
corresponding to each wildcard (or capture group if RegExp object provided) and the
`hostname` that was matched.

Where the `hostname` of the request comes from depends on the type of server you're running.
If you're running a raw Node.js/connect server, this comes from [`req.headers.host`](https://nodejs.org/dist/latest/docs/api/http.html#http_message_headers).
If you're running an express v3 server, this comes from [`req.host`](http://expressjs.com/en/3x/api.html#req.host).
If you're running an express v4 server, this comes from [`req.hostname`](http://expressjs.com/en/4x/api.html#req.hostname).

```js
var connect = require('connect')
var vhost = require('vhost')
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ function vhost (hostname, handle) {
*/

function hostnameof (req) {
var host = req.headers.host
var host = req.hostname || // express v4
req.host || // express v3
req.headers.host // http

if (!host) {
return
Expand Down
80 changes: 78 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

var assert = require('assert')
var http = require('http')
var request = require('supertest')
Expand All @@ -22,6 +21,44 @@ describe('vhost(hostname, server)', function () {
.expect(200, 'tobi', done)
})

it('should route by `req.hostname` (express v4)', function (done) {
var vhosts = []

vhosts.push(vhost('anotherhost.com', anotherhost))
vhosts.push(vhost('loki.com', loki))

var app = createServer(vhosts, null, function (req) {
req.hostname = 'anotherhost.com'
})

function anotherhost (req, res) { res.end('anotherhost') }
function loki (req, res) { res.end('loki') }

request(app)
.get('/')
.set('Host', 'tobi.com')
.expect(200, 'anotherhost', done)
})

it('should route by `req.host` (express v3)', function (done) {
var vhosts = []

vhosts.push(vhost('anotherhost.com', anotherhost))
vhosts.push(vhost('loki.com', loki))

var app = createServer(vhosts, null, function (req) {
req.host = 'anotherhost.com'
})

function anotherhost (req, res) { res.end('anotherhost') }
function loki (req, res) { res.end('loki') }

request(app)
.get('/')
.set('Host', 'tobi.com')
.expect(200, 'anotherhost', done)
})

it('should ignore port in Host', function (done) {
var app = createServer('tobi.com', function (req, res) {
res.end('tobi')
Expand All @@ -44,6 +81,42 @@ describe('vhost(hostname, server)', function () {
.expect(200, 'loopback', done)
})

it('should support IPv6 literal in `req.host` with port (express v5)', function (done) {
var app = createServer('[::1]', function (req, res) {
res.end('loopback')
}, function (req) {
req.host = '[::1]:8080'
})

request(app)
.get('/')
.expect(200, 'loopback', done)
})

it('should support IPv6 literal in `req.hostname` (express v4)', function (done) {
var app = createServer('[::1]', function (req, res) {
res.end('loopback')
}, function (req) {
req.hostname = '[::1]'
})

request(app)
.get('/')
.expect(200, 'loopback', done)
})

it('should support IPv6 literal in `req.host` without port (express v3)', function (done) {
var app = createServer('[::1]', function (req, res) {
res.end('loopback')
}, function (req) {
req.host = '[::1]'
})

request(app)
.get('/')
.expect(200, 'loopback', done)
})

it('should 404 unless matched', function (done) {
var vhosts = []

Expand Down Expand Up @@ -215,12 +288,15 @@ describe('vhost(hostname, server)', function () {
})
})

function createServer (hostname, server) {
function createServer (hostname, server, pretest) {
var vhosts = !Array.isArray(hostname)
? [vhost(hostname, server)]
: hostname

return http.createServer(function onRequest (req, res) {
// This allows you to perform changes to the request/response
// objects before our assertions
if (pretest) pretest(req, res)
var index = 0

function next (err) {
Expand Down