import http from "http"; const host = '127.0.0.1'; const port = 8000; const users = { "joe": { name: "Joe User", sticker_ids: [], username: "joe", }, "jen": { name: "Jen User", sticker_ids: ["38dafe9b-5563-4112-afa8-c895adc68e5b"], username: "jen", } }; const stickers = { "88ae126f-b19a-4287-abe0-a8f5ac763cb7": { color: "#000088", layout: "layout1", message1: "I ♥ Medicare all All", owner: null, uuid: "88ae126f-b19a-4287-abe0-a8f5ac763cb7", }, "38dafe9b-5563-4112-afa8-c895adc68e5b": { color: "#008800", layout: "layout1", message1: "I am a treehugger!", owner: null, uuid: "38dafe9b-5563-4112-afa8-c895adc68e5b", }, } const requestListener = function (req, res) { res.setHeader("Content-Type", "application/json"); const urlParts = req.url.split("/"); console.log(urlParts); let body = ''; req.on('data', chunk => { body += chunk.toString(); }); req.on('end', () => { if (body) { body = JSON.parse(body); } console.log('body', body); switch (urlParts[1]) { case "login": res.writeHead(200); res.end(JSON.stringify(users[body["username"]])); break; case "sticker": if (!(urlParts[2] in stickers)) { res.writeHead(404); res.end('not found') break; } res.writeHead(200); const sticker = stickers[urlParts[2]]; const response = JSON.stringify(sticker); res.end(response); break; case "user": if (!(urlParts[2] in users)) { res.writeHead(404); res.end('not found') break; } res.writeHead(200); res.end(JSON.stringify(users[urlParts[2]])); break; default: res.writeHead(200); res.end(JSON.stringify({nothing: 'here'})); } }); }; const server = http.createServer(requestListener); server.listen(port, host, () => { console.log(`Server is running on http://${host}:${port}`); });