1
0
Fork 0
activist/server.js

59 lines
1.3 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 user = JSON.stringify({
name: "Joe User",
2024-11-26 11:30:20 -05:00
stickers: [],
username: "joe",
2024-11-24 20:50:39 -05:00
});
2024-11-25 20:06:22 -05:00
const stickers = {
"88ae126f-b19a-4287-abe0-a8f5ac763cb7": {
2024-11-26 08:51:46 -05:00
color: "#000088",
2024-11-25 20:06:22 -05:00
layout: "layout1",
message1: "I am an activist!",
owner: null,
2024-11-26 11:30:20 -05:00
uuid: "88ae126f-b19a-4287-abe0-a8f5ac763cb7",
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-11-26 11:30:20 -05:00
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
console.log('body', body);
});
2024-11-25 20:06:22 -05:00
console.log(urlParts);
switch (urlParts[1]) {
case "login":
2024-11-24 20:50:39 -05:00
res.writeHead(200);
res.end(user);
2024-11-25 20:06:22 -05:00
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;
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}`);
});