1- import { createServer } from 'http' ;
1+ import { createServer , IncomingMessage , ServerResponse } from 'http' ;
22import { createFactory } from 'react' ;
33import * as ReactDomServer from 'react-dom/server' ;
44import { readFile } from 'fs' ;
@@ -18,59 +18,65 @@ const readFileAsync = promisify(readFile);
1818createServer ( async ( req , res ) => {
1919 const { httpVersion, method, url } = req ;
2020 console . log ( `${ httpVersion } ${ method } ${ url } ` ) ;
21- if ( url === '/' ) {
22-
23- res . setHeader ( 'Content-Type' , 'text/html' ) ;
24- res . write ( '<!DOCTYPE html>' ) ;
25- res . write ( `<html>
26- <head>
27- <meta charset="utf-8" />
28- <meta name="viewport" content="width=device-width, initial-scale=1.0">
29- <link href="${ faviconUrl } " rel="icon" type="image/x-icon" />
30- <title>React Example</title>
31- <link rel="stylesheet" href="${ stylesUrl } " />
32- </head>
33- <body>
34- <div id="${ containerId } ">` ) ;
35- const props : AppProps = { items : getItems ( ) } ;
36- const renderToNodeStream = ReactDomServer . renderToNodeStream as any ;
37- const stream = renderToNodeStream ( App ( props ) ) as any ;
38- stream . pipe ( res , { end : false } ) ;
39- stream . on ( 'end' , ( ) => {
40- res . end ( `</div>
41- <script src="${ reactUrl } "></script>
42- <script src="${ reactDomUrl } "></script>
43- <script src="${ browserUrl } "></script>
44- </body>
45- </html>` ) ;
46- } ) ;
47- } else if ( url === propsUrl ) {
48- const items = getItems ( ) ;
49- const props = { items : items } ;
50- res . setHeader ( 'Content-Type' , 'application/json' ) ;
51- res . end ( JSON . stringify ( props ) ) ;
52- } else if ( url === reactUrl ) {
53- res . setHeader ( 'Content-Type' , 'text/javascript' ) ;
54- res . setHeader ( 'Cache-Control' , 'public, max-age=86400' ) ;
55- const data = await readFileAsync ( `./node_modules/react/umd/react${ suffix } ` ) ;
56- res . end ( data ) ;
57- } else if ( url === reactDomUrl ) {
58- res . setHeader ( 'Content-Type' , 'text/javascript' ) ;
59- res . setHeader ( 'Cache-Control' , 'public, max-age=86400' ) ;
60- const data = await readFileAsync ( `./node_modules/react-dom/umd/react-dom${ suffix } ` ) ;
61- res . end ( data ) ;
62- } else if ( url === stylesUrl ) {
63- res . setHeader ( 'Content-Type' , 'text/css' ) ;
64- const data = await readFileAsync ( './src/style.css' ) ;
65- res . end ( data ) ;
66- } else if ( url === browserUrl || url === browserMapUrl ) {
67- res . setHeader ( 'Content-Type' , 'text/javascript' ) ;
68- const data = await readFileAsync ( `./dist${ url } ` ) ;
69- res . end ( data ) ;
70- } else {
21+ try {
22+ if ( url === '/' ) {
23+ res . setHeader ( 'Content-Type' , 'text/html' ) ;
24+ res . write ( `<!DOCTYPE html>
25+ <html>
26+ <head>
27+ <meta charset="utf-8" />
28+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
29+ <link href="${ faviconUrl } " rel="icon" type="image/x-icon" />
30+ <title>React Example</title>
31+ <link rel="stylesheet" href="${ stylesUrl } " />
32+ </head>
33+ <body>
34+ <div id="${ containerId } ">` ) ;
35+ const props : AppProps = { items : getItems ( ) } ;
36+ const renderToNodeStream = ReactDomServer . renderToNodeStream as any ;
37+ const stream = renderToNodeStream ( App ( props ) ) as any ;
38+ stream . pipe ( res , { end : false } ) ;
39+ stream . on ( 'end' , ( ) => {
40+ res . end ( `</div>
41+ <script src="${ reactUrl } "></script>
42+ <script src="${ reactDomUrl } "></script>
43+ <script src="${ browserUrl } "></script>
44+ </body>
45+ </html>` ) ;
46+ } ) ;
47+ } else if ( url === propsUrl ) {
48+ const items = getItems ( ) ;
49+ const props : AppProps = { items : items } ;
50+ res . setHeader ( 'Content-Type' , 'application/json' ) ;
51+ res . end ( JSON . stringify ( props ) ) ;
52+ } else if ( url === reactUrl ) {
53+ res . setHeader ( 'Content-Type' , 'text/javascript' ) ;
54+ res . setHeader ( 'Cache-Control' , 'public, max-age=86400' ) ;
55+ const data = await readFileAsync ( `./node_modules/react/umd/react${ suffix } ` ) ;
56+ res . end ( data ) ;
57+ } else if ( url === reactDomUrl ) {
58+ res . setHeader ( 'Content-Type' , 'text/javascript' ) ;
59+ res . setHeader ( 'Cache-Control' , 'public, max-age=86400' ) ;
60+ const data = await readFileAsync ( `./node_modules/react-dom/umd/react-dom${ suffix } ` ) ;
61+ res . end ( data ) ;
62+ } else if ( url === stylesUrl ) {
63+ res . setHeader ( 'Content-Type' , 'text/css' ) ;
64+ const data = await readFileAsync ( `./src/${ url } ` ) ;
65+ res . end ( data ) ;
66+ } else if ( url === browserUrl || url === browserMapUrl ) {
67+ res . setHeader ( 'Content-Type' , 'text/javascript' ) ;
68+ const data = await readFileAsync ( `./dist${ url } ` ) ;
69+ res . end ( data ) ;
70+ } else {
71+ res . setHeader ( 'Content-Type' , 'text/plain' ) ;
72+ res . statusCode = 404 ;
73+ res . end ( '404 Not Found' ) ;
74+ }
75+ } catch ( e ) {
76+ console . error ( e ) ;
7177 res . setHeader ( 'Content-Type' , 'text/plain' ) ;
72- res . statusCode = 404 ;
73- res . end ( '404 Not Found ' ) ;
78+ res . statusCode = 500 ;
79+ res . end ( '500 Internal Error ' ) ;
7480 }
7581} ) . listen ( PORT , ( ) => {
7682 console . log ( `Listening on ${ PORT } ...` )
0 commit comments