-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
70 lines (57 loc) · 1.59 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const express = require('express');
const uniqid = require('uniqid');
const app = express();
const fs = require('fs');
const path = require('path');
const { notes } = require('./db/db.json');
const PORT = process.env.PORT || 3001;
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static('public'));
const createNewNote = (note, id) => {
const newNote = {
title: note.title,
text: note.text,
id: id
};
notes.push(newNote);
fs.writeFileSync(
path.join(__dirname + "/db/db.json"),
JSON.stringify({ notes: notes }, null, 2)
)
}
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + './public/index.html'));
})
app.get('/notes', (req, res) => {
res.sendFile(path.join(__dirname, './public/notes.html'));
})
app.get('/api/notes', (req, res) => {
res.json(notes);
})
app.post('/api/notes', ({ body }, res) => {
const id = uniqid();
createNewNote(body, id);
res.send("success");
})
app.delete('/api/notes/:id', (req, res) => {
let noteToRemoveIndex;
for (let i = 0; i < notes.length; i++) {
if (notes[i].id === req.params.id) {
noteToRemoveIndex = i;
}
}
if (noteToRemoveIndex === undefined) {
res.status(404).send("Not Found");
return;
}
notes.splice(noteToRemoveIndex, 1);
fs.writeFileSync(
path.join(__dirname, "/db/db.json"),
JSON.stringify({ notes: notes }, null, 2)
)
res.status(200).send("success");
})
app.listen(PORT, () => {
console.log(`API server listening on port ${PORT}`);
})