-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (36 loc) · 1.53 KB
/
server.js
File metadata and controls
51 lines (36 loc) · 1.53 KB
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
const express = require('express');
const path = require('path');
//todo: add in api and change it to routes/index.js
const db = require('./Develop/db/db.json');
const fs = require('fs');
const PORT = process.env.PORT || 3001;
const app = express();
const uuid = require('./Develop/helpers/uuid')
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static('Develop/public'));
//GET Route for home page
app.get('/', (req,res) =>
res.sendFile(path.join(__dirname, 'Develop/public/index.html'))
);
//GET Route for notes page
app.get('/notes', (req,res) =>
res.sendFile(path.join(__dirname, 'Develop/public/notes.html'))
);
app.get("/api/notes", (req, res) => {
res.sendFile(path.join(__dirname, "./Develop/db/db.json"));
});
app.post('/api/notes', (req, res) => {
console.info(`${req.method} request received to add a note`);
let newNote = req.body; //the note is = to the body
let noteList = JSON.parse(fs.readFileSync("./Develop/db/db.json", "utf8")); // generates a list of all notes and saves it to a var by reading current db
//creates unique ids using uuid, woo
newNote.id = uuid();
noteList.push(newNote); //pushes our new note onto that array read from the db
//write the updated data to db.json
fs.writeFileSync("./Develop/db/db.json", JSON.stringify(noteList)); //writes the list with newly added note, JSON's it and writes over the old db.
res.json(noteList);
});
app.listen(PORT, () =>
console.log(`App listening at http://localhost:${PORT} 🚀`)
);