-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
84 lines (64 loc) · 1.88 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
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const app = express();
app.set("view engine","ejs");
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static("public"));
mongoose.connect("mongodb://todolist-db:eo4gPt2xqy4qaXdTmo9KMyhlJTErHhBa3Pj2HMrP0xNZThv4ffU4Byu9pTFeoxYhS5hvB6mrMMB3eUidFD6bwA%3D%3D@todolist-db.mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@todolist-db@/todolistDB");
const itemsSchema = {
name: String
};
const Item = mongoose.model("Item",itemsSchema);
const item1 = new Item({
name:"Welcome to todolist"
});
const item2 = new Item({
name:"Hit + to add new item"
});
const item3 = new Item({
name:"tick the checkbox to delete that item"
});
const defaultitems=[item1,item2,item3];
app.get("/",function(req,res){
var today = new Date();
var options = { weekday: 'long', day: 'numeric',month: 'long' };
var day = today.toLocaleDateString("en-US", options);
Item.find({},function(err,items){
if (items.length===0){
Item.insertMany(defaultitems,function(err){
if (err){
console.log(err);
}else{
console.log("Successfully added default items");
}
res.redirect("/");
});
}
else{
res.render("list",{kindofday:day,ListItems:items});
}
});
});
app.post("/",function(req,res){
const item = req.body.newItem
const additem = new Item({
name: item
});
additem.save();
res.redirect("/");
});
app.post("/delete",function(req,res){
const checkbox_id = req.body.checkbox
Item.deleteOne({_id:checkbox_id},function(err){
if(err){
console.log(err);
}else{
console.log("successfully deleted");
}
})
res.redirect("/")
});
app.listen(8080,function(){
console.log("Server started on port 8080");
});