-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (91 loc) · 2.7 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
101
102
103
104
105
const e = require('express');
const express = require('express');
// PORT NUMBER AT WHICH OUR SERVER WILL BE RUNNING
const port = 8000;
// CREATE TO THE DATABASE
const db = require('./config/mongoose');
// CREATE OBJECT OF EXPRESS
const app = express();
// GET THE PATH OBJECT TO SET THE PATH OF CSS FILE (OR JAVASCRIPT FILE)
const path = require('path');
// IMPORT THE TODO OBJECT FROM MODELS
const Todo = require('./models/todo');
// FOR DECODING URL
app.use(express.urlencoded());
// USING EJS AS TEMPLE ENGINE
app.set('view engine', 'ejs');
// SET THE PATH OF VIEWS DIRECTORY
app.set('views', path.join(__dirname, 'views'));
// USE THE CSS AND JAVASCRIPT FILE
app.use(express.static('assets'));
// THIS IS HOME PAGE URL
app.get('/', function (req, res) {
Todo.find({}, function (err, todos) {
if (err) {
console.log('error', err);
return;
}
return res.render('home',
{
title: "TODO APP",
todo_list: todos
}
);
});
});
// THIS IS URL FOR CREATING THE TASK IN DATABASE
app.post('/create-todo', function (req, res) {
Todo.create({
description: req.body.description,
category: req.body.category,
date: req.body.date
}, function (err, newtodo) {
if (err) {
console.log('error in creating task', err);
return;
}
return res.redirect('back');
}
)
});
// THIS IS DELETE URL FOR SINGLE TASK FROM DATABASE
app.get('/delete_todo_single', function(req, res) {
let id = req.query.id;
Todo.findByIdAndDelete(id, function(err){
if(err) {
console.log("error");
return;
}
return res.redirect('back');
});
});
// THIS IS URL TO DELETE THE MULTIPLE ITEM FROM DATABASE
app.post('/delete-todo', function(req, res) {
let ids = req.body.task;
// if single task is to be deleted
if (typeof(ids) == "string") {
Todo.findByIdAndDelete(ids, function(err) {
if (err) {
console.log("error in deleting");
return;
}
});
} else { // if multiple task is to be deleted
for (let i = 0; i < ids.length; i++) {
Todo.findByIdAndDelete(ids[i], function (err) {
if (err) {
console.log("error in deleting");
return;
}
});
}
}
return res.redirect('back');
});
// SERVER
app.listen(port, function(err) {
if(err) {
console.log("Error in setting up the express server!");
}
console.log("Express server is up and running on port:", port);
});