File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+ const http = require ( 'http' ) ;
3+ /* `createServer` MUST return an instance of `http.Server` otherwise the tests
4+ * will fail.
5+ */
6+ function createServer ( port ) {
7+ let state = 10 ;
8+ const server = http . createServer ( ( request , response ) => {
9+ // TODO: Write your homework code here
10+ switch ( request . url ) {
11+ case '/state' :
12+ response . writeHead ( 200 , { 'Content-Type' : 'application/json' } ) ;
13+ response . end ( JSON . stringify ( { state : state } ) ) ;
14+ break ;
15+ case '/add' :
16+ state ++ ;
17+ response . writeHead ( 200 , { 'Content-Type' : 'application/json' } ) ;
18+ response . end ( JSON . stringify ( { state : state } ) ) ;
19+ break ;
20+ case '/subtract' :
21+ state -- ;
22+ response . writeHead ( 200 , { 'Content-Type' : 'application/json' } ) ;
23+ response . end ( JSON . stringify ( { state : state } ) ) ;
24+ break ;
25+ case '/reset' :
26+ state = 10 ;
27+ response . writeHead ( 200 , { 'Content-Type' : 'application/json' } ) ;
28+ response . end ( JSON . stringify ( { state : state } ) ) ;
29+ break ;
30+ default :
31+ response . writeHead ( 404 , { 'Content-Type' : 'application/json' } ) ;
32+ response . end ( JSON . stringify ( { error : 'Not found' } ) ) ;
33+ }
34+ } ) ;
35+ return server ;
36+ }
37+ module . exports = {
38+ createServer
39+ } ;
You can’t perform that action at this time.
0 commit comments