diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ace0b35..8f59862 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/README.md b/README.md index 422e67f..102ae16 100644 --- a/README.md +++ b/README.md @@ -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') diff --git a/index.js b/index.js index 480fd19..82c39d2 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/test/test.js b/test/test.js index 1e41c8d..68d5b98 100644 --- a/test/test.js +++ b/test/test.js @@ -1,4 +1,3 @@ - var assert = require('assert') var http = require('http') var request = require('supertest') @@ -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') @@ -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 = [] @@ -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) {