-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (76 loc) · 1.9 KB
/
index.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
90
91
92
93
94
95
96
97
98
99
100
//jshint esversion:6
require('dotenv').config()
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const { getDate } = require("./date");
const date = require(__dirname + "/date.js");
const app = express();
const port=process.env.PORT || 3000;
app.set("view engine","ejs");
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static("public"));
//yaha se mongodb ka part suru hai
const mongoURI = process.env.mongoURI
mongoose.connect(mongoURI,{useNewUrlParser:true});
const itemsSchema={
name:String
};
const Itema=mongoose.model("Itema",itemsSchema);
const item1=new Itema({
name: "Welcome to your toDoList!"
});
const item2=new Itema({
name:"Lets start"
});
const defaultItems=[item1,item2];
app.get("/",function(req,res)
{
Itema.find({},function(err,foundItems){
var day= getDate();
if(foundItems.length==0){
Itema.insertMany(defaultItems,function(err){
if(err){
console.log(err);
}else{
console.log("Succesfully saved default items to DB.");
}
});
res.redirect("/");
}
else{
res.render("lists",{listTitle:day,newListItems:foundItems});
}
});
});
//for adding from input
app.post("/",function(req,res)
{
const itemName=req.body.newItem;
const item=new Itema({
name:itemName
});
item.save();
console.log("Successfully added.");
res.redirect("/");
});
//for deleting from db
app.post("/delete",function(req,res){
const checkedItemId=req.body.checkbox;
Itema.findByIdAndRemove(checkedItemId,function(err){
if(!err){
console.log("Successfully deleted checked item.");
res.redirect("/");
}
});
});
app.get("/work",function(req,res){
res.render("lists",{listTitle:"WorkList"});
})
app.get("/about",function(req,res)
{
res.render("about");
})
app.listen(port, function(){
console.log("Server started successfully");
});