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

Commit fe4ea4c

Browse files
Add middleware for patching Express request object
1 parent 1efc286 commit fe4ea4c

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

packages/gluestick/src/commands/__tests__/start-client.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ describe('commands/start-client', () => {
115115
it('should start express with webpack dev and hot middlewares', () => {
116116
startClientCommand(commandApi, [{}]);
117117
waitUntilValidCallback();
118-
expect(middlewares.length).toBe(3);
118+
expect(middlewares.length).toBe(4);
119119
const res = { render: jest.fn() };
120120
proxyOnErrorCallback(null, {}, res);
121121
expect(res.render.mock.calls[0][0].includes('poll.html')).toBeTruthy();

packages/gluestick/src/commands/start-client.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const fs = require('fs');
66
const webpack = require('webpack');
77
const express = require('express');
88
const proxy = require('http-proxy-middleware');
9+
10+
const patchRequestHost = require('../lib/patchRequestHost');
911
const progressHandler = require('../config/webpack/progressHandler');
1012
const { printWebpackStats, debounce } = require('../utils');
1113
const build = require('./build');
@@ -115,6 +117,7 @@ module.exports = (
115117
},
116118
);
117119
app.set('view engine', 'html');
120+
app.use(patchRequestHost());
118121
const devMiddleware = require('webpack-dev-middleware')(
119122
compiler,
120123
developmentServerOptions,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Apply a patch to req.host to fix a bug that exists in Express v4.
3+
*
4+
* Specifically, without this patch, req.host will only return the host _name_
5+
* and not the full host including the port number.
6+
*
7+
* This patch currently exists in Express v5, which is also currently alpha.
8+
*
9+
* For more information, see:
10+
*
11+
* https://expressjs.com/en/guide/migrating-5.html#req.host
12+
* https://github.com/expressjs/express/blob/4.16.4/lib/request.js#L448-L452
13+
* https://github.com/expressjs/express/blob/5.0.0-alpha.7/lib/request.js#L395-L415
14+
*/
15+
function patchRequestHost () {
16+
return function patchRequestHost (req, res, next) {
17+
defineGetter(req, 'host', function host (){
18+
var trust = this.app.get('trust proxy fn');
19+
var val = this.get('X-Forwarded-Host');
20+
21+
if (!val || !trust(this.connection.remoteAddress, 0)) {
22+
val = this.get('Host');
23+
}
24+
25+
return val || undefined;
26+
});
27+
28+
next();
29+
};
30+
}
31+
32+
function defineGetter(obj, name, getter) {
33+
Object.defineProperty(obj, name, {
34+
configurable: true,
35+
enumerable: true,
36+
get: getter
37+
});
38+
}
39+
40+
module.exports = patchRequestHost;

packages/gluestick/src/renderer/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const onFinished = require('on-finished');
1919
const applicationConfig = require('application-config').default;
2020
const entries = require('project-entries').default;
2121

22+
const patchRequestHost = require('../lib/patchRequestHost');
2223
const serverPlugins = require('../plugins/serverPlugins');
2324
const createPluginUtils = require('../plugins/utils');
2425
const setProxies = require('./helpers/setProxies');

0 commit comments

Comments
 (0)