1
0
Fork 0

Parse body.

This commit is contained in:
Brian D 2024-12-03 20:40:16 -05:00
parent 0b43f42c85
commit 178e87239e
2 changed files with 37 additions and 29 deletions

View file

@ -36,42 +36,46 @@ const stickers = {
const requestListener = function (req, res) { const requestListener = function (req, res) {
res.setHeader("Content-Type", "application/json"); res.setHeader("Content-Type", "application/json");
const urlParts = req.url.split("/"); const urlParts = req.url.split("/");
console.log(urlParts);
let body = ''; let body = '';
req.on('data', chunk => { req.on('data', chunk => {
body += chunk.toString(); body += chunk.toString();
}); });
req.on('end', () => { req.on('end', () => {
if (body) {
body = JSON.parse(body);
}
console.log('body', 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'}));
}
}); });
console.log(urlParts);
switch (urlParts[1]) {
case "login":
res.writeHead(200);
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); const server = http.createServer(requestListener);

View file

@ -21,10 +21,14 @@ const next = route.query.next;
function handleLogin({ valid }) { function handleLogin({ valid }) {
if (!valid) { if (!valid) {
console.log('Input is not valid.')
return; return;
} }
sessionStore.login(username.value, username.value).then(() => { sessionStore.login(username.value, username.value).then(() => {
console.log(`go to ${next}`)
router.push(next); router.push(next);
}).catch((error) => {
console.log(error);
}); });
} }