11import { createServer , IncomingMessage , ServerResponse } from 'http' ;
22import { createFactory } from 'react' ;
33import { renderToNodeStream } from 'react-dom/server' ;
4+ import { createReadStream } from 'fs' ;
45import App from './components/app' ;
56import { fetchProps } from './props' ;
7+ import { lookup } from './mime-types' ;
8+ import { control } from './cache-control' ;
69import {
710 faviconUrl ,
811 stylesUrl ,
@@ -13,7 +16,6 @@ import {
1316 propsUrl ,
1417 containerId ,
1518} from './constants' ;
16- import { readFileAsync } from './file' ;
1719
1820console . log ( 'Server booting...' ) ;
1921const isProd = process . env . NODE_ENV === 'production' ;
@@ -23,11 +25,15 @@ const PORT = process.env.PORT || 3007;
2325const suffix = isProd ? '.production.min.js' : '.development.js' ;
2426
2527createServer ( async ( req , res ) => {
26- const { httpVersion, method, url } = req ;
28+ let { httpVersion, method, url } = req ;
2729 console . log ( `${ httpVersion } ${ method } ${ url } ` ) ;
30+ if ( ! url || url === '/' ) {
31+ url = 'index.html' ;
32+ }
2833 try {
29- if ( url === '/' ) {
30- res . setHeader ( 'Content-Type' , 'text/html' ) ;
34+ if ( url === 'index.html' ) {
35+ res . setHeader ( 'Content-Type' , lookup ( url ) ) ;
36+ res . setHeader ( 'Cache-Control' , control ( isProd , 1 ) ) ;
3137 res . write ( `<!DOCTYPE html>
3238 <html>
3339 <head>
@@ -50,34 +56,37 @@ createServer(async (req, res) => {
5056 </html>` ) ;
5157 } ) ;
5258 } else if ( url === propsUrl ) {
53- res . setHeader ( 'Content-Type' , 'application/json' ) ;
59+ res . setHeader ( 'Content-Type' , lookup ( url ) ) ;
60+ res . setHeader ( 'Cache-Control' , control ( isProd , 0 ) ) ;
5461 res . end ( JSON . stringify ( fetchProps ( ) ) ) ;
55- } else if ( url === reactUrl ) {
56- res . setHeader ( 'Content-Type' , 'text/javascript' ) ;
57- res . setHeader ( 'Cache-Control' , 'public, max-age=86400' ) ;
58- const data = await readFileAsync ( `./node_modules/react/umd/react${ suffix } ` ) ;
59- res . end ( data ) ;
60- } else if ( url === reactDomUrl ) {
61- res . setHeader ( 'Content-Type' , 'text/javascript' ) ;
62- res . setHeader ( 'Cache-Control' , 'public, max-age=86400' ) ;
63- const data = await readFileAsync ( `./node_modules/react-dom/umd/react-dom${ suffix } ` ) ;
64- res . end ( data ) ;
62+ } else if ( url === reactUrl || url === reactDomUrl ) {
63+ res . setHeader ( 'Content-Type' , lookup ( url ) ) ;
64+ res . setHeader ( 'Cache-Control' , control ( isProd , 7 ) ) ;
65+ const name = url . replace ( '.js' , '' ) ;
66+ const file = `./node_modules${ name } /umd${ name } ${ suffix } ` ;
67+ createReadStream ( file ) . pipe ( res ) ;
6568 } else if ( url === stylesUrl ) {
66- res . setHeader ( 'Content-Type' , 'text/css' ) ;
67- const data = await readFileAsync ( `./src/${ url } ` ) ;
68- res . end ( data ) ;
69+ res . setHeader ( 'Content-Type' , lookup ( url ) ) ;
70+ res . setHeader ( 'Cache-Control' , control ( isProd , 7 ) ) ;
71+ const file = `./src/${ url } ` ;
72+ createReadStream ( file ) . pipe ( res ) ;
6973 } else if ( url === browserUrl || url === browserMapUrl ) {
70- res . setHeader ( 'Content-Type' , 'text/javascript' ) ;
71- const data = await readFileAsync ( `./dist${ url } ` ) ;
72- res . end ( data ) ;
74+ res . setHeader ( 'Content-Type' , lookup ( url ) ) ;
75+ res . setHeader ( 'Cache-Control' , control ( isProd , 7 ) ) ;
76+ const file = `./dist${ url } ` ;
77+ createReadStream ( file ) . pipe ( res ) ;
7378 } else {
74- res . setHeader ( 'Content-Type' , 'text/plain' ) ;
79+ url = 'notfound.txt' ;
80+ res . setHeader ( 'Content-Type' , lookup ( url ) ) ;
81+ res . setHeader ( 'Cache-Control' , control ( isProd , 0 ) ) ;
7582 res . statusCode = 404 ;
7683 res . end ( '404 Not Found' ) ;
7784 }
7885 } catch ( e ) {
7986 console . error ( e ) ;
80- res . setHeader ( 'Content-Type' , 'text/plain' ) ;
87+ url = 'notfound.txt' ;
88+ res . setHeader ( 'Content-Type' , lookup ( url ) ) ;
89+ res . setHeader ( 'Cache-Control' , control ( isProd , 0 ) ) ;
8190 res . statusCode = 500 ;
8291 res . end ( '500 Internal Error' ) ;
8392 }
0 commit comments