Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ed79cc3

Browse files
authored
Create index.js
1 parent c004823 commit ed79cc3

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Week1/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
};

0 commit comments

Comments
 (0)