-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
37 lines (26 loc) · 943 Bytes
/
api.js
File metadata and controls
37 lines (26 loc) · 943 Bytes
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
const uuid = require("uuid");
const fs = require("fs");
const router = require('express').Router();
router.get('/notes', (req,res) => {
const data = fs.readFileSync("./db/db.json");
res.json(JSON.parse(data));
})
router.post("/notes", (req, res) => {
const {title, text} = req.body;
const newNote = {title, text};
newNote.id = uuid.v4();
// console.log(newNote);
// note.push(newNote);
var allNotes = JSON.parse(fs.readFileSync("./db/db.json"));
allNotes.push(newNote)
fs.writeFileSync("./db/db.json", JSON.stringify(allNotes));
console.log(allNotes);
res.json(allNotes)
})
router.delete('/notes/:id', (req, res) => {
const note = JSON.parse(ds.readFileSync('./db/db.json'));
const deleteNote = note.filter((rmvNote) => rmvNote.id !== req.params.id);
fs.writeFileSync('./db/db.json', JSON.stringify(deleteNote));
res.json(deleteNote);
})
module.exports = router;