From 93351bf8451cb112a6a04307199c70d158a14fa9 Mon Sep 17 00:00:00 2001 From: jeromew Date: Tue, 7 Jan 2014 19:01:11 +0000 Subject: [PATCH] Add req.host= --- docs/api/request.md | 6 ++++++ lib/request.js | 11 +++++++++++ test/request/host.js | 11 ++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/docs/api/request.md b/docs/api/request.md index 1f1d21e..9e43f63 100644 --- a/docs/api/request.md +++ b/docs/api/request.md @@ -60,6 +60,12 @@ Get host void of port number when present. Supports `X-Forwarded-Host` when `app.proxy` is __true__, otherwise `Host` is used. +### req.host= + + Set host to a new value. It sets the `Host` header under the hood and will + behave correctly with req.subdomains. + This can be useful for a `hostOverride()` middleware + ### req.type Get request `Content-Type` void of parameters such as "charset". diff --git a/lib/request.js b/lib/request.js index c5893bb..4625113 100644 --- a/lib/request.js +++ b/lib/request.js @@ -189,6 +189,17 @@ module.exports = { return host.split(/\s*,\s*/)[0].split(':')[0]; }, + /** + * Set the host. + * + * @param {String} str + * @api public + */ + + set host(val) { + this.req.headers.host = val; + }, + /** * Check if the request is fresh, aka * Last-Modified and/or the ETag diff --git a/test/request/host.js b/test/request/host.js index 5c80303..ee29d2c 100644 --- a/test/request/host.js +++ b/test/request/host.js @@ -28,4 +28,13 @@ describe('req.host', function(){ }) }) }) -}) \ No newline at end of file +}) + +describe('req.host=', function(){ + it('should replace the host', function(){ + var req = request(); + req.header['host'] = 'foo.com'; + req.host = 'bar.com'; + req.host.should.equal('bar.com') + }) +})