Updated server.mjs with 58 additions and 59 removals
--- a/server.mjs
+++ b/server.mjs
@@ -13,62 +13,61 @@
 // Read data from db.json, this will set db.data content
 // If db.json doesn't exist, it'll be created with default data
 await db.read();
-await db.read();
-if (!db.data) {
-  db.data = { notes: [] };
-  await db.write();
-}
-
-app.use(express.json());
-app.use(express.static('public'));
-
-// Get all notes
-app.get('/api/notes', (req, res) => {
-  res.json(db.data.notes);
-});
-
-// Get a single note by ID
-app.get('/api/notes/:id', (req, res) => {
-  const note = db.data.notes.find(n => n.id === req.params.id);
-  if (note) {
-    res.json(note);
-  } else {
-    res.status(404).send('Note not found');
-  }
-});
-
-// Add a new note
-app.post('/api/notes', async (req, res) => {
-  const newNote = { id: nanoid(), content: req.body.content };
-  db.data.notes.push(newNote);
-  await db.write();
-  res.status(201).json(newNote);
-});
-
-// Update a note
-app.put('/api/notes/:id', async (req, res) => {
-  const index = db.data.notes.findIndex(n => n.id === req.params.id);
-  if (index !== -1) {
-    db.data.notes[index].content = req.body.content;
-    await db.write();
-    res.json(db.data.notes[index]);
-  } else {
-    res.status(404).send('Note not found');
-  }
-});
-
-// Delete a note
-app.delete('/api/notes/:id', async (req, res) => {
-  const initialLength = db.data.notes.length;
-  db.data.notes = db.data.notes.filter(n => n.id !== req.params.id);
-  if (db.data.notes.length < initialLength) {
-    await db.write();
-    res.status(204).send();
-  } else {
-    res.status(404).send('Note not found');
-  }
-});
-
-app.listen(port, () => {
-  console.log(`Server listening at http://localhost:${port}`);
-});
+if (!db.data) {
+  db.data = { notes: [] };
+  await db.write();
+}
+
+app.use(express.json());
+app.use(express.static('public'));
+
+// Get all notes
+app.get('/api/notes', (req, res) => {
+  res.json(db.data.notes);
+});
+
+// Get a single note by ID
+app.get('/api/notes/:id', (req, res) => {
+  const note = db.data.notes.find(n => n.id === req.params.id);
+  if (note) {
+    res.json(note);
+  } else {
+    res.status(404).send('Note not found');
+  }
+});
+
+// Add a new note
+app.post('/api/notes', async (req, res) => {
+  const newNote = { id: nanoid(), content: req.body.content };
+  db.data.notes.push(newNote);
+  await db.write();
+  res.status(201).json(newNote);
+});
+
+// Update a note
+app.put('/api/notes/:id', async (req, res) => {
+  const index = db.data.notes.findIndex(n => n.id === req.params.id);
+  if (index !== -1) {
+    db.data.notes[index].content = req.body.content;
+    await db.write();
+    res.json(db.data.notes[index]);
+  } else {
+    res.status(404).send('Note not found');
+  }
+});
+
+// Delete a note
+app.delete('/api/notes/:id', async (req, res) => {
+  const initialLength = db.data.notes.length;
+  db.data.notes = db.data.notes.filter(n => n.id !== req.params.id);
+  if (db.data.notes.length < initialLength) {
+    await db.write();
+    res.status(204).send();
+  } else {
+    res.status(404).send('Note not found');
+  }
+});
+
+app.listen(port, () => {
+  console.log(`Server listening at http://localhost:${port}`);
+});