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,17 +36,20 @@ 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);
});
console.log(urlParts);
switch (urlParts[1]) { switch (urlParts[1]) {
case "login": case "login":
res.writeHead(200); res.writeHead(200);
res.end(JSON.stringify(users[body["username"]]));
break; break;
case "sticker": case "sticker":
if (!(urlParts[2] in stickers)) { if (!(urlParts[2] in stickers)) {
@ -72,6 +75,7 @@ const requestListener = function (req, res) {
res.writeHead(200); res.writeHead(200);
res.end(JSON.stringify({nothing: 'here'})); 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);
}); });
} }