-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
66 lines (47 loc) · 1.53 KB
/
script.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
//Declear the installed modules express and body-parser.
let express = require('express');
let bodyParser = require('body-parser');
let note = [{ id: 1, body: 'We have a text' }, { id: 2, body: 'This is a second text' }];
//call the express and Body-parser
let app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static('public'));
//we installed the ejs and created a file inside the views
app.set('view engine', 'ejs');
//We set up the route for the App. We first use the app.get option.
app.get('/', function (req, res) {
res.render('notes', {
note: note
});
});
//then, we use app.post option.
app.post("/addNotes", function (req, res) {
//assigning Note id to the notes using math.random
const userNote = {};
userNote.id = Math.random() * 100;
userNote.body = req.body.newNote
note.push(userNote);
//then we redirect it to the root route
res.redirect('/');
});
//Handling the edit request
app.post('/editNote/:id', function (req, res) {
console.log(req.params.id);
console.log(note);
const editNotes = note.item => item.id = req.params.id);
note = editNotes;
res.redirect('/');
});
//Handling the delete request
app.post('/deleteNote/:id', function (req, res) {
console.log(req.params.id);
const deleteNotes = note.filter(item => item.id != req.params.id);
note = deleteNotes;
return res.redirect('/');
});
//then we set our server port. This should always be at bottom.
app.listen(5000, function () {
console.log("NoteApp server is running at port 5000...")
});