1
0
Fork 0
activist/server.js

85 lines
2 KiB
JavaScript
Raw Normal View History

2024-11-24 20:50:39 -05:00
import http from "http";
2024-11-25 20:06:22 -05:00
const host = '127.0.0.1';
2024-11-24 20:50:39 -05:00
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",
}
};
2024-11-24 20:50:39 -05:00
2024-11-25 20:06:22 -05:00
const stickers = {
"88ae126f-b19a-4287-abe0-a8f5ac763cb7": {
color: "780606",
2024-11-25 20:06:22 -05:00
layout: "layout1",
2024-12-06 19:51:38 -05:00
message1: "I ♥ Medicare All",
owner: null,
2024-11-26 11:30:20 -05:00
uuid: "88ae126f-b19a-4287-abe0-a8f5ac763cb7",
2024-11-25 20:06:22 -05:00
},
"38dafe9b-5563-4112-afa8-c895adc68e5b": {
color: "008800",
layout: "layout1",
message1: "I am a treehugger!",
owner: null,
uuid: "38dafe9b-5563-4112-afa8-c895adc68e5b",
},
2024-11-25 20:06:22 -05:00
}
2024-11-24 20:50:39 -05:00
const requestListener = function (req, res) {
res.setHeader("Content-Type", "application/json");
2024-11-25 20:06:22 -05:00
const urlParts = req.url.split("/");
2024-12-03 20:40:16 -05:00
console.log(urlParts);
2024-11-26 11:30:20 -05:00
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
2024-12-03 20:40:16 -05:00
if (body) {
body = JSON.parse(body);
}
2024-11-26 11:30:20 -05:00
console.log('body', body);
2024-12-03 20:40:16 -05:00
switch (urlParts[1]) {
case "login":
res.writeHead(200);
res.end(JSON.stringify(users[body["username"]]));
2024-11-25 20:06:22 -05:00
break;
2024-12-03 20:40:16 -05:00
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;
2024-12-03 20:40:16 -05:00
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'}));
}
});
2024-11-24 20:50:39 -05:00
};
const server = http.createServer(requestListener);
server.listen(port, host, () => {
console.log(`Server is running on http://${host}:${port}`);
});