File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ // create web server
2
+ // 1. Create a web server
3
+ // 2. Create a route
4
+ // 3. Create a response
5
+
6
+ // 1. Create a web server
7
+ const http = require ( 'http' ) ;
8
+ const server = http . createServer ( ) ;
9
+ server . listen ( 3000 ) ;
10
+
11
+ // 2. Create a route
12
+ server . on ( 'request' , ( req , res ) => {
13
+ if ( req . url === '/comments' ) {
14
+ res . writeHead ( 200 , { 'Content-Type' : 'application/json' } ) ;
15
+ res . write ( JSON . stringify ( { comments : [ ] } ) ) ;
16
+ res . end ( ) ;
17
+ }
18
+ else {
19
+ res . writeHead ( 404 ) ;
20
+ res . end ( ) ;
21
+ }
22
+ } ) ;
23
+ // 3. Create a response
24
+ // http://localhost:3000/comments
25
+ // GET /comments
26
+ // 200 OK
27
+ // Content-Type: application/json
28
+ // { "comments": [] }
29
+
30
+ // http://localhost:3000/unknown
31
+ // GET /unknown
32
+ // 404 Not Found
You can’t perform that action at this time.
0 commit comments