-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (74 loc) · 2.15 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
/*
* Copyright (c) 2020
* All rights reserved.
*/
// set the express
const express = require('express');
// create path directory
const path = require('path');
// making the port
const port = 8000;
// require mongodb database
const db = require('./config/mongoose');
// require models schema
const ToDo = require('./models/toDo');
// for express app
const app = express();
// set the templates engine as ejs
app.set('view engine', 'ejs');
// set the directory for views
app.set('views', path.join(__dirname, 'views'));
app.use(express.urlencoded());
// using static file (assets file)
app.use(express.static('assets'));
// render the tasklist on browser
app.get('/', function(req, res){
ToDo.find({}, function(error, taskList){
if(error)
{
console.log('Error in fetching from database', error);
return
}
return res.render('toodletodo',{
titleName: 'ToodleDo | Todo-App',
task_list: taskList,
});
});
});
// creating the task
app.post('/create-task', function(req, res){
// creating task in database and showing on browser
ToDo.create({
description: req.body.description,
due_date: req.body.due_date,
category: req.body.category
}, function(error, newTask){
if(error){
console.log('Error in creating a taskList', error);
return;
}
console.log('Hurray!!',newTask);
res.redirect('back');
});
});
// deleting tht task
app.get('/delete-task/', function(req, res){
console.log(req.query)
let id = req.query.id
ToDo.findByIdAndDelete(id,function(error){
if(error){
console.log('error in deleting the object from the database');
return;
}
return res.redirect('back')
});
});
// starting the server
app.listen(port, function(error){
if(error){
console.log('Error in running the server', error);
}
else{
console.log('Sever running successfully on the port:', port);
}
});