-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
100 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,79 @@ | ||
import { createServer } from "node:http"; | ||
import { createServer, IncomingMessage } from "node:http"; | ||
|
||
let todos = []; | ||
|
||
const server = createServer((req, res) => { | ||
// handle get request | ||
if (req.method === "GET") { | ||
res.setHeader("Content-Type", "text/html"); | ||
res.statusCode = 200; | ||
res.end(renderPage()); | ||
} else if (req.method === "POST") { | ||
let body = ""; | ||
req.on("data", chunk => { | ||
body += chunk.toString(); | ||
}); | ||
req.on("end", () => { | ||
// implement adding of todo or deleting | ||
res.statusCode = 501; | ||
res.end("not implemented"); | ||
}); | ||
} | ||
// handle get request | ||
if (req.method === "GET") { | ||
res.setHeader("Content-Type", "text/html"); | ||
res.statusCode = 200; | ||
res.end(renderPage()); | ||
} else if (req.method === "POST" && req.url === "/add") { | ||
parseBody(req).then((params) => { | ||
res.statusCode = 501; | ||
res.end("not implemented"); | ||
}); | ||
} | ||
}); | ||
|
||
function renderPage() { | ||
return `<!doctype html> | ||
return `<!doctype html> | ||
<html> | ||
<head> | ||
<title>Todo App</title> | ||
<meta charset="utf-8"> | ||
</heade> | ||
<body> | ||
<h1>Todo App</h1> | ||
<form method="POST" action="/"> | ||
<form method="POST" action="/add"> | ||
<input type="text" name="todo" placeholder="Enter a new todo"> | ||
<button type="submit">Add</button> | ||
</form> | ||
<ul> | ||
${todos.map(todo => `<li>${todo}</li>`).join("")} | ||
${todos.map((todo) => `<li>${todo}</li>`).join("")} | ||
</ul> | ||
</body> | ||
</html> | ||
` | ||
|
||
`; | ||
} | ||
|
||
server.listen(3000, () => { | ||
console.log("Server is running on http://localhost:3000"); | ||
console.log("Server is running on http://localhost:3000"); | ||
}); | ||
|
||
// some helpers to parse the query part from a URL or parse the body from a request | ||
|
||
/** | ||
* @param url {string} | ||
* @returns { Promise<{[name:string]:string}> } | ||
*/ | ||
function parseQuery(url) { | ||
const query = url.split("?")[1] ?? ""; | ||
const result = query.split("&").reduce((prev, curr) => { | ||
const keyValue = curr.split("="); | ||
prev[keyValue[0]] = keyValue[1]; | ||
return prev; | ||
}, {}); | ||
return result; | ||
} | ||
|
||
/** | ||
* @param req { IncomingMessage } | ||
* @returns { Promise<{[name:string]:string}> } | ||
*/ | ||
function parseBody(req) { | ||
return new Promise((resolve) => { | ||
let buffer = ""; | ||
req.on("data", (chunk) => { | ||
buffer += chunk; | ||
}); | ||
req.on("end", () => { | ||
const result = buffer.split("\n").reduce((prev, curr) => { | ||
const keyValue = curr.split("="); | ||
prev[keyValue[0]] = keyValue[1]; | ||
return prev; | ||
}, {}); | ||
resolve(result); | ||
}); | ||
}); | ||
} |