diff --git a/week1/homework/src/server.js b/week1/homework/src/server.js index 5aea6a470..cdb739938 100644 --- a/week1/homework/src/server.js +++ b/week1/homework/src/server.js @@ -10,7 +10,31 @@ function createServer(port) { const server = http.createServer((request, response) => { // TODO: Write your homework code here + if (req.method === 'GET' && req.url === '/state') { + res.writeHead(200, { 'Content-Type' : 'application/json'}); + res.end(JSON.stringify({state: state})); + } + else if (req.method === 'GET' && req.url === '/reset') { + state = 10; + res.writeHead(200, { 'Content-Type' : 'application/json'}); + res.end(JSON.stringify({ state:state})); + } + else if (req.method === 'GET' && req.url === '/add') { + state++; + res.writeHead(200, { 'Content-Type': 'application/json'}); + res.end(JSON.stringify({state: state})); + } + else if (req.method === 'GET' && req.url === '/subtract') { + state--; + res.writeHead(200, { 'Content-Type' : 'application/json'}); + res.end(JSON.stringify({state: state})); + } + else { + res.writeHead(404, {'Content-Type': 'application/json'}); + res.end(JSON.stringify({error: 'Not found'})); + } }); + return server; }