33const express = require ( 'express' ) ;
44const basicAuth = require ( './basic-auth' ) ;
55
6+ function noop ( ) { }
7+
68class ExpressHTTPServer {
79 constructor ( options ) {
810 options = options || { } ;
@@ -13,18 +15,23 @@ class ExpressHTTPServer {
1315 this . password = options . password ;
1416 this . cache = options . cache ;
1517 this . gzip = options . gzip || false ;
18+ this . beforeMiddleware = options . beforeMiddleware || noop ;
19+ this . afterMiddleware = options . afterMiddleware || noop ;
1620
1721 this . app = express ( ) ;
18- if ( options . gzip ) {
19- this . app . use ( require ( 'compression' ) ( ) ) ;
20- }
2122 }
2223
23- serve ( middleware ) {
24+ serve ( fastbootMiddleware ) {
2425 let app = this . app ;
2526 let username = this . username ;
2627 let password = this . password ;
2728
29+ this . beforeMiddleware ( app ) ;
30+
31+ if ( this . gzip ) {
32+ this . app . use ( require ( 'compression' ) ( ) ) ;
33+ }
34+
2835 if ( username !== undefined || password !== undefined ) {
2936 this . ui . writeLine ( `adding basic auth; username=${ username } ; password=${ password } ` ) ;
3037 app . use ( basicAuth ( username , password ) ) ;
@@ -35,14 +42,16 @@ class ExpressHTTPServer {
3542 }
3643
3744 if ( this . distPath ) {
38- app . get ( '/' , middleware ) ;
45+ app . get ( '/' , fastbootMiddleware ) ;
3946 app . use ( express . static ( this . distPath ) ) ;
4047 app . get ( '/assets/*' , function ( req , res ) {
4148 res . sendStatus ( 404 ) ;
4249 } ) ;
4350 }
4451
45- app . get ( '/*' , middleware ) ;
52+ app . get ( '/*' , fastbootMiddleware ) ;
53+
54+ this . afterMiddleware ( app ) ;
4655
4756 return new Promise ( resolve => {
4857 let listener = app . listen ( process . env . PORT || 3000 , ( ) => {
0 commit comments