File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Create web server
2
+ // Load HTTP module
3
+ const http = require ( "http" ) ;
4
+ const fs = require ( "fs" ) ;
5
+ const url = require ( "url" ) ;
6
+ const path = require ( "path" ) ;
7
+ const port = 3000 ;
8
+
9
+ const server = http . createServer ( ( req , res ) => {
10
+ console . log ( req . url ) ;
11
+ if ( req . url === "/" ) {
12
+ fs . readFile ( "./index.html" , function ( error , data ) {
13
+ res . writeHead ( 200 , { "Content-Type" : "text/html" } ) ;
14
+ res . write ( data ) ;
15
+ res . end ( ) ;
16
+ } ) ;
17
+ } else if ( req . url === "/comments" ) {
18
+ fs . readFile ( "./comments.json" , function ( error , data ) {
19
+ res . writeHead ( 200 , { "Content-Type" : "application/json" } ) ;
20
+ res . write ( data ) ;
21
+ res . end ( ) ;
22
+ } ) ;
23
+ } else if ( req . url === "/comments.js" ) {
24
+ fs . readFile ( "./comments.js" , function ( error , data ) {
25
+ res . writeHead ( 200 , { "Content-Type" : "text/javascript" } ) ;
26
+ res . write ( data ) ;
27
+ res . end ( ) ;
28
+ } ) ;
29
+ } else {
30
+ res . writeHead ( 404 , { "Content-Type" : "text/html" } ) ;
31
+ res . write ( "<h1>Page not found</h1>" ) ;
32
+ res . end ( ) ;
33
+ }
34
+ } ) ;
35
+
36
+ server . listen ( port , ( ) => {
37
+ console . log ( `Server running at http://localhost:${ port } /` ) ;
38
+ } ) ;
You can’t perform that action at this time.
0 commit comments