summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/command.js8
-rw-r--r--src/server.js43
-rw-r--r--src/template.html11
3 files changed, 60 insertions, 2 deletions
diff --git a/src/command.js b/src/command.js
index c022c81..b2f7091 100644
--- a/src/command.js
+++ b/src/command.js
@@ -8,6 +8,7 @@ import {
removeNote,
} from "./notes.js";
import { listNotes } from "./utils.js";
+import { start } from "./server.js";
yargs(hideBin(process.argv))
.command(
@@ -78,13 +79,16 @@ yargs(hideBin(process.argv))
type: "number",
});
},
- async (argv) => {},
+ async (argv) => {
+ const notes = await getAllNotes();
+ start(notes, argv.port);
+ },
)
.command(
"clean",
"remove all notes",
() => {},
- async (argv) => {
+ async () => {
await removeAllNotes();
console.log("db reseted");
},
diff --git a/src/server.js b/src/server.js
new file mode 100644
index 0000000..eff1e38
--- /dev/null
+++ b/src/server.js
@@ -0,0 +1,43 @@
+import fs from "node:fs/promises";
+import http from "node:http";
+import open from "open";
+
+const interpolate = (html, data) => {
+ return html.replace(/\{\{\s*(\w+)\s*\}\}/g, (match, placeholder) => {
+ return data[placeholder] || "";
+ });
+};
+
+const formatNotes = (notes) => {
+ return notes
+ .map((note) => {
+ return `
+ <div class="note">
+ <p>${note.content}</p>
+ <div class="tags">
+ ${note.tags.map((tag) => `<span class="tag">${tag}</span>`).join("")}
+ </div>
+ </div>
+ `;
+ })
+ .join("\n");
+};
+
+const createServer = (notes) => {
+ return http.createServer(async (_, res) => {
+ const HTML_PATH = new URL("./template.html", import.meta.url).pathname;
+ const template = await fs.readFile(HTML_PATH, "utf-8");
+ const html = interpolate(template, { notes: formatNotes(notes) });
+
+ res.writeHead(200, { "content-type": "text/html" });
+ res.end(html);
+ });
+};
+
+export const start = (notes, port) => {
+ const server = createServer(notes);
+ server.listen(port, () => {
+ console.log(`Server ist listening on port ${port}`);
+ });
+ open(`http://localhost:${port}`);
+};
diff --git a/src/template.html b/src/template.html
new file mode 100644
index 0000000..c8e9f47
--- /dev/null
+++ b/src/template.html
@@ -0,0 +1,11 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="UTF-8" />
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
+ <title>Notes</title>
+ </head>
+ <body>
+ <div class="notes">{{ notes }}</div>
+ </body>
+</html>