-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
89 lines (87 loc) · 1.86 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
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
89
var express=require("express");
var mongoose=require("mongoose");
var bodyParser=require("body-parser");
var app=express();
app.set('view engine','ejs');
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended:true}));
mongoose.connect("mongodb://localhost:27017/todolistDB",{useNewUrlParser:true,useUnifiedTopology:true});
const itemSchema={
name:String
}
const Item=mongoose.model("Item",itemSchema);
const item1=new Item({
name:"List of Works to be Done today",
});
const item2=new Item({
name:"Practice hard coz practise makes a man perfect",
});
const item3=new Item({
name:"Enjoy learning",
});
const d=[item1,item2,item3];
/*
Item.insertMany(d,function(err)
{
if(err){
console.log(err);
}
else{
console.log("Successfully saved items to DB");
}
});
*/
app.get("/",function(req,res)
{
// res.send("<h1>Hey guys!!</h1>");
Item.find({},function(err,f)
{
// console.log(f);
if(f.length===0)
{
Item.insertMany(d,function(err)
{
if(err){
console.log(err);
}
else{
console.log("Successfully saved items to DB");
}
});
res.redirect("/");
}
else{
res.render("list",{newListItems:f});
}
})
;
})
app.post("/",function(req,res)
{
const itemName=req.body.n;
//console.log(i);
//i1.push(i);
//res.render("list",{newListItem:i});
// res.redirect("/");
const item=new Item({
name:itemName
});
item.save();
res.redirect("/");
});
app.post("/delete",function(req,res)
{
const check=req.body.checkbox;
Item.findByIdAndRemove(check,function(err)
{
if(!err)
{
console.log("Successfully deleted");
res.redirect("/");
}
})
});
app.listen(3000,function()
{
console.log("Server is listening to port 3000");
});