From a5b50b422850e938a3f0908da81cbdf5ce4d24c3 Mon Sep 17 00:00:00 2001 From: Adeeba <124208667+AdeebaSaeed@users.noreply.github.com> Date: Mon, 15 May 2023 21:10:55 +0200 Subject: [PATCH] Update server.js --- week1/homework/src/server.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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; }