-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (51 loc) · 1.47 KB
/
index.js
File metadata and controls
59 lines (51 loc) · 1.47 KB
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
// Setting up the server
const express = require('express');
const app = express();
const port = 8080;
// To connect to the DB
const db = require('./config/mongoose');
// Connecting the Schema
const Tasklist = require('./models/tasklist');
//Middle ware
app.use(express.urlencoded());// It is a middle ware
app.use(express.static('assets'));
//To connect to the routes
app.use('/', require('./routes'));
//Setting view engine
app.set('view engine','ejs');
app.set('views', './views');
//To create task
app.post('/create-task', function(req,res){
Tasklist.create({
description: req.body.description,
date:req.body.date,
category:req.body.category
},function(err,newTask){
if(err){
console.log("Error in adding the task");
return;
}
console.log('*******',newTask);
return res.redirect('back');
})
})
// To Delete the task
app.get('/delete-task', function(req,res){
var id = req.query;
var count =Object.keys(id).length;
for(let i=0; i<count ;i++){
Tasklist.findByIdAndDelete(Object.keys(id)[i], function(err){
if(err){
console.log('error in deleteing an object from database');
}
});
}
return res.redirect('back');
});
// To print server running status
app.listen(port, function(err){
if(err){
console.log(`Error in running the server:${port}`);
}
console.log(`Yup! My Express Server is running on Port:${port}`);
});