-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (55 loc) · 1.65 KB
/
app.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
const express = require('express');
const path = require('path');
const bodyPaser = require('body-parser');
const app = express();
// configuration : setting view engine
app.set('view engine','ejs');
app.set('views',path.join(__dirname,'views'));
// middleware :
app.use(bodyPaser());
// array declaration {id:1,task:"exercise"},
var todoList = [];
//-------------------------------------------------------------------------
// default route
app.get("/",(req,res)=>{
//render view : display html file
res.render('index',{
title : "My Todo App",
items : todoList
});
res.end();
});
//--------------------------------------------------------------------------
// route to add element
app.post("/add",(req,res)=>{
//read value from post data
var newTask = req.body.textfield;
// add element into the list
todoList.push({
id : todoList.length+1,
task : newTask
});
//render view : display html file
res.render('index',{
title : "My Todo App",
items : todoList
});
res.end();
});
//-------------------------------------------------------------------------
// route to delete element
app.post("/delete",(req,res)=>{
//delete element from the list
todoList.pop();
//render view : display html file
res.render('index',{
title : "My Todo App",
items : todoList
});
res.end();
});
//------------------------------------------------------------------------
// start server at 3000
app.listen(3000,()=>{
console.log("TodoApp started @ localhost:3000 ");
});