11'use strict' ;
22
3-
43function fastbootExpressMiddleware ( distPath , options ) {
54 let opts = options ;
65
@@ -22,51 +21,51 @@ function fastbootExpressMiddleware(distPath, options) {
2221 let FastBoot = require ( 'fastboot' ) ;
2322 fastboot = new FastBoot ( {
2423 distPath : opts . distPath ,
25- resilient : opts . resilient
24+ resilient : opts . resilient ,
2625 } ) ;
2726 }
2827
2928 return function ( req , res , next ) {
3029 let path = req . url ;
31- fastboot . visit ( path , { request : req , response : res } )
32- . then ( success , failure ) ;
30+ fastboot . visit ( path , { request : req , response : res } ) . then ( success , failure ) ;
3331
3432 function success ( result ) {
3533 let responseBody = opts . chunkedResponse ? result . chunks ( ) : result . html ( ) ;
3634
37- responseBody . then ( body => {
38- let headers = result . headers ;
39- let statusMessage = result . error ? 'NOT OK ' : 'OK ' ;
40-
41- for ( var pair of headers . entries ( ) ) {
42- res . set ( pair [ 0 ] , pair [ 1 ] ) ;
43- }
44-
45- if ( result . error ) {
46- log ( "RESILIENT MODE CAUGHT:" , result . error . stack ) ;
47- next ( result . error ) ;
48- }
49-
50- log ( result . statusCode , statusMessage + path ) ;
51- res . status ( result . statusCode ) ;
52-
53- if ( typeof body === 'string' ) {
54- res . send ( body ) ;
55- } else if ( result . error ) {
56- res . send ( body [ 0 ] ) ;
57- } else {
58- body . forEach ( chunk => res . write ( chunk ) ) ;
59- res . end ( ) ;
60- }
61- } )
62- . catch ( error => {
63- res . status ( 500 ) ;
64- next ( error ) ;
65- } ) ;
35+ responseBody
36+ . then ( body => {
37+ let headers = result . headers ;
38+ let statusMessage = result . error ? 'NOT OK ' : 'OK ' ;
39+
40+ for ( var pair of headers . entries ( ) ) {
41+ res . set ( pair [ 0 ] , pair [ 1 ] ) ;
42+ }
43+
44+ if ( result . error ) {
45+ log ( 'RESILIENT MODE CAUGHT:' , result . error . stack ) ;
46+ next ( result . error ) ;
47+ }
48+
49+ log ( result . statusCode , statusMessage + path ) ;
50+ res . status ( result . statusCode ) ;
51+
52+ if ( typeof body === 'string' ) {
53+ res . send ( body ) ;
54+ } else if ( result . error ) {
55+ res . send ( body [ 0 ] ) ;
56+ } else {
57+ body . forEach ( chunk => res . write ( chunk ) ) ;
58+ res . end ( ) ;
59+ }
60+ } )
61+ . catch ( error => {
62+ res . status ( 500 ) ;
63+ next ( error ) ;
64+ } ) ;
6665 }
6766
6867 function failure ( error ) {
69- if ( error . name === " UnrecognizedURLError" ) {
68+ if ( error . name === ' UnrecognizedURLError' ) {
7069 next ( ) ;
7170 } else {
7271 res . status ( 500 ) ;
@@ -85,10 +84,10 @@ function _log(statusCode, message, startTime) {
8584
8685 if ( startTime ) {
8786 let diff = Date . now ( ) - startTime ;
88- message = message + chalk . blue ( " " + diff + "ms" ) ;
87+ message = message + chalk . blue ( ' ' + diff + 'ms' ) ;
8988 }
9089
91- console . log ( chalk . blue ( now . toISOString ( ) ) + " " + chalk [ color ] ( statusCode ) + " " + message ) ;
90+ console . log ( chalk . blue ( now . toISOString ( ) ) + ' ' + chalk [ color ] ( statusCode ) + ' ' + message ) ;
9291}
9392
9493module . exports = fastbootExpressMiddleware ;
0 commit comments