-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (66 loc) · 2.05 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
// require express for setting up the express server
const express = require('express');
// set up the port number
const port = 7000;
// importing the DataBase
const db = require('./config/mongoose');
// importng the Schema For tasks
const Task = require('./models/task');
// using express
const app = express();
// using static files
app.use(express.static("./views"));
// to use encrypted data
app.use(express.urlencoded());
// set up the view engine
app.set('view engine', 'ejs');
app.set('views', './views');
// rendering the App Page
app.get('/', function(req, res){
Task.find({}, function(err, task){
if(err){
console.log('Error in fetching tasks from db');
return;
}
return res.render('app', {
tittle: "app",
task: task
});
}
)});
// creating Tasks
app.post('/create-task', function(req, res){
// console.log("Creating Task");
Task.create({
description: req.body.description,
category: req.body.category,
date: req.body.date
}, function(err, newtask){
if(err){console.log('error in creating task', err); return;}
//console.log(newtask);
return res.redirect('back');
});
});
// deleting Tasks
app.get('/delete-task', function(req, res){
// get the id from query
var id = req.query;
// checking the number of tasks selected to delete
var count = Object.keys(id).length;
for(let i=0; i < count ; i++){
// finding and deleting tasks from the DB one by one using id
Task.findByIdAndDelete(Object.keys(id)[i], function(err){
if(err){
console.log('error in deleting task');
}
})
}
return res.redirect('back');
});
// make the app to listen on asigned port number
app.listen(port, function(err){
if(err){
console.log(`Error in running the server : ${err}`);
}
console.log(`Server is running on port : ${port}`);
});