-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
88 lines (73 loc) · 2.29 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const express = require('express');
const fs = require('fs');
const { verifyNote , verifyPathId } = require('./controller')
const PORT = 3000;
const app = express();
//
app.use(express.static('./static'))
app.use(express.json())
let notes = [];
fs.readFile('./database/db.json' , (err , data) => {
if(err)
return console.log(err)
notes = JSON.parse(data).Notes;
})
app.get('/notes' , (req , res) => {
if(notes === null)
return res.status(500).json({msg : 'error on the server'})
res.status(200).json(notes)
})
app.post('/notes' , verifyNote ,(req , res) => {
const {title , body} = req.body;
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const dateToSave = [year, month, day].join('-');
fs.readFile('./database/db.json' , (err , data) => {
if(err)
return console.log(err)
let notesInfo = JSON.parse(data);
let noteToSave = {
id: notesInfo.lastId,
title,
dateOfCreation: dateToSave,
noteBody: body
}
notesInfo.Notes.push(noteToSave);
notesInfo.lastId++;
fs.writeFile('./database/db.json' , JSON.stringify(notesInfo , null , 2) , (err) => {
if(err)
return res.status(500).json({msg : "error will writing on the server"})
res.status(201).json(noteToSave)
})
})
})
app.delete('/notes/:id', verifyPathId ,(req , res) => {
console.log('delete');
const id = req.params.id;
console.log(id);
fs.readFile('./database/db.json' , (err , data) => {
if(err)
return console.log(err)
let notesInfo = JSON.parse(data);
let notesData = notesInfo.Notes;
const noteToDelete = notesData.findIndex((note) => note.id == id );
console.log(noteToDelete);
if(noteToDelete === -1)
return res.status(404).json({msg : "note not found"})
let notesToSave = notesData.filter((note) => {
return note.id != id
})
console.log(notesToSave);
notesInfo.Notes = notesToSave;
fs.writeFile('./database/db.json' , JSON.stringify(notesInfo , null , 2) , (err) => {
if(err)
return res.status(500).json({msg : "error will writing on the server"})
res.status(200).json({msg : "note was deleted"})
})
})
})
app.listen(PORT , () => {
console.log('server listening on port: ' + PORT)
})