11import { createServer } from 'http' ;
22import * as React from 'react' ;
33import { renderToString } from 'react-dom/server' ;
4- import * as fs from 'fs' ;
4+ import { readFile } from 'fs' ;
5+ import { promisify } from 'util' ;
56import AppComponent from './components/app' ;
67import { getItems } from './db' ;
78import { faviconUrl , stylesUrl , reactUrl , reactDomUrl , browserUrl , browserMapUrl , propsUrl , containerId } from './constants' ;
@@ -10,12 +11,14 @@ console.log('Server booting...');
1011const isProd = process . env . NODE_ENV === 'production' ;
1112console . log ( 'Production optimization enabled? ' , isProd ) ;
1213const App = React . createFactory ( AppComponent ) ;
13- const PORT = 3007 ;
14+ const PORT = process . env . PORT || 3007 ;
1415const suffix = isProd ? '.production.min.js' : '.development.js' ;
16+ const readFileAsync = promisify ( readFile ) ;
1517
16- createServer ( ( req , res ) => {
17- console . log ( `${ req . httpVersion } ${ req . method } ${ req . url } ` ) ;
18- if ( req . url === '/' ) {
18+ createServer ( async ( req , res ) => {
19+ const { httpVersion, method, url } = req ;
20+ console . log ( `${ httpVersion } ${ method } ${ url } ` ) ;
21+ if ( url === '/' ) {
1922 const props : AppProps = { items : getItems ( ) } ;
2023 const reactHtml = renderToString ( App ( props ) ) ;
2124 const pageHtml = `<!DOCTYPE html>
@@ -36,49 +39,34 @@ createServer((req, res) => {
3639 </html>` ;
3740 res . setHeader ( 'Content-Type' , 'text/html' ) ;
3841 res . end ( pageHtml )
39- } else if ( req . url === propsUrl ) {
42+ } else if ( url === propsUrl ) {
4043 const items = getItems ( ) ;
4144 const props = { items : items } ;
4245 res . setHeader ( 'Content-Type' , 'application/json' ) ;
4346 res . end ( JSON . stringify ( props ) ) ;
44- } else if ( req . url === reactUrl ) {
47+ } else if ( url === reactUrl ) {
4548 res . setHeader ( 'Content-Type' , 'text/javascript' ) ;
4649 res . setHeader ( 'Cache-Control' , 'public, max-age=86400' ) ;
47- fs . readFile ( `./node_modules/react/umd/react${ suffix } ` , ( err , data ) => {
48- if ( err ) { console . error ( err ) ; }
49- res . end ( data ) ;
50- } ) ;
51- } else if ( req . url === reactDomUrl ) {
50+ const data = await readFileAsync ( `./node_modules/react/umd/react${ suffix } ` ) ;
51+ res . end ( data ) ;
52+ } else if ( url === reactDomUrl ) {
5253 res . setHeader ( 'Content-Type' , 'text/javascript' ) ;
5354 res . setHeader ( 'Cache-Control' , 'public, max-age=86400' ) ;
54- fs . readFile ( `./node_modules/react-dom/umd/react-dom${ suffix } ` , ( err , data ) => {
55- if ( err ) { console . error ( err ) ; }
56- res . end ( data ) ;
57- } ) ;
58- } else if ( req . url === stylesUrl ) {
55+ const data = await readFileAsync ( `./node_modules/react-dom/umd/react-dom${ suffix } ` ) ;
56+ res . end ( data ) ;
57+ } else if ( url === stylesUrl ) {
5958 res . setHeader ( 'Content-Type' , 'text/css' ) ;
60- fs . readFile ( './src/style.css' , ( err , data ) => {
61- if ( err ) { console . error ( err ) ; }
62- res . end ( data ) ;
63- } ) ;
64- } else if ( req . url === browserUrl ) {
59+ const data = await readFileAsync ( './src/style.css' ) ;
60+ res . end ( data ) ;
61+ } else if ( url === browserUrl || url === browserMapUrl ) {
6562 res . setHeader ( 'Content-Type' , 'text/javascript' ) ;
66- fs . readFile ( './dist/browser.js' , ( err , data ) => {
67- if ( err ) { console . error ( err ) ; }
68- res . end ( data ) ;
69- } ) ;
70- } else if ( req . url === browserMapUrl ) {
71- res . setHeader ( 'Content-Type' , 'text/javascript' ) ;
72- fs . readFile ( './dist/browser.js.map' , ( err , data ) => {
73- if ( err ) { console . error ( err ) ; }
74- res . end ( data ) ;
75- } ) ;
63+ const data = await readFileAsync ( `./dist${ url } ` ) ;
64+ res . end ( data ) ;
7665 } else {
7766 res . setHeader ( 'Content-Type' , 'text/plain' ) ;
7867 res . statusCode = 404 ;
79- res . end ( 'Not Found' ) ;
68+ res . end ( '404 Not Found' ) ;
8069 }
81-
8270} ) . listen ( PORT , ( ) => {
8371 console . log ( `Listening on ${ PORT } ...` )
8472} ) ;
0 commit comments