Skip to content
This repository has been archived by the owner on Aug 5, 2020. It is now read-only.

Commit

Permalink
Add middleware for patching Express request object
Browse files Browse the repository at this point in the history
  • Loading branch information
christinebrass committed Jan 16, 2019
1 parent 1efc286 commit fe4ea4c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('commands/start-client', () => {
it('should start express with webpack dev and hot middlewares', () => {
startClientCommand(commandApi, [{}]);
waitUntilValidCallback();
expect(middlewares.length).toBe(3);
expect(middlewares.length).toBe(4);
const res = { render: jest.fn() };
proxyOnErrorCallback(null, {}, res);
expect(res.render.mock.calls[0][0].includes('poll.html')).toBeTruthy();
Expand Down
3 changes: 3 additions & 0 deletions packages/gluestick/src/commands/start-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const fs = require('fs');
const webpack = require('webpack');
const express = require('express');
const proxy = require('http-proxy-middleware');

const patchRequestHost = require('../lib/patchRequestHost');
const progressHandler = require('../config/webpack/progressHandler');
const { printWebpackStats, debounce } = require('../utils');
const build = require('./build');
Expand Down Expand Up @@ -115,6 +117,7 @@ module.exports = (
},
);
app.set('view engine', 'html');
app.use(patchRequestHost());
const devMiddleware = require('webpack-dev-middleware')(
compiler,
developmentServerOptions,
Expand Down
40 changes: 40 additions & 0 deletions packages/gluestick/src/lib/patchRequestHost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Apply a patch to req.host to fix a bug that exists in Express v4.
*
* Specifically, without this patch, req.host will only return the host _name_
* and not the full host including the port number.
*
* This patch currently exists in Express v5, which is also currently alpha.
*
* For more information, see:
*
* https://expressjs.com/en/guide/migrating-5.html#req.host
* https://github.com/expressjs/express/blob/4.16.4/lib/request.js#L448-L452
* https://github.com/expressjs/express/blob/5.0.0-alpha.7/lib/request.js#L395-L415
*/
function patchRequestHost () {
return function patchRequestHost (req, res, next) {
defineGetter(req, 'host', function host (){
var trust = this.app.get('trust proxy fn');
var val = this.get('X-Forwarded-Host');

if (!val || !trust(this.connection.remoteAddress, 0)) {
val = this.get('Host');
}

return val || undefined;
});

next();
};
}

function defineGetter(obj, name, getter) {
Object.defineProperty(obj, name, {
configurable: true,
enumerable: true,
get: getter
});
}

module.exports = patchRequestHost;
1 change: 1 addition & 0 deletions packages/gluestick/src/renderer/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const onFinished = require('on-finished');
const applicationConfig = require('application-config').default;
const entries = require('project-entries').default;

const patchRequestHost = require('../lib/patchRequestHost');
const serverPlugins = require('../plugins/serverPlugins');
const createPluginUtils = require('../plugins/utils');
const setProxies = require('./helpers/setProxies');
Expand Down

0 comments on commit fe4ea4c

Please sign in to comment.