This repository was archived by the owner on Aug 5, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +45
-1
lines changed
Expand file tree Collapse file tree 4 files changed +45
-1
lines changed Original file line number Diff line number Diff 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 ( ) ;
Original file line number Diff line number Diff line change @@ -6,6 +6,8 @@ const fs = require('fs');
66const webpack = require ( 'webpack' ) ;
77const express = require ( 'express' ) ;
88const proxy = require ( 'http-proxy-middleware' ) ;
9+
10+ const patchRequestHost = require ( '../lib/patchRequestHost' ) ;
911const progressHandler = require ( '../config/webpack/progressHandler' ) ;
1012const { printWebpackStats, debounce } = require ( '../utils' ) ;
1113const 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 ,
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ const onFinished = require('on-finished');
1919const applicationConfig = require ( 'application-config' ) . default ;
2020const entries = require ( 'project-entries' ) . default ;
2121
22+ const patchRequestHost = require ( '../lib/patchRequestHost' ) ;
2223const serverPlugins = require ( '../plugins/serverPlugins' ) ;
2324const createPluginUtils = require ( '../plugins/utils' ) ;
2425const setProxies = require ( './helpers/setProxies' ) ;
You can’t perform that action at this time.
0 commit comments