-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
99 lines (86 loc) · 2.44 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const bodyParser = require('body-parser')
require('dotenv').config();
// support parsing of application/json type post data
app.use(bodyParser.json());
//ejs jade
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
//support parsing of application/x-www-form-urlencoded post data
app.use(bodyParser.urlencoded({ extended: true }));
const uri = process.env.mongoURI;
mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
console.log('MongoDB Connected…')
})
.catch(err => console.log(err));
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server started on port ${port}`));
//Schema
var Todo = new mongoose.Schema({
name: String,
priority: String
});
var Todo = mongoose.model("Todo", Todo);
//temporary for sending alert
message=false;
//Get method
app.get('/',(req,res,next) =>{
//Here fetch data using mongoose query like
Todo.find({}, function(err, tasks) {
if (err) throw err;
// object of all the users
res.render(__dirname + '/index.html', { Todo:tasks ,idTask:'' ,message } );
message=false;
});
});
// POST method route
app.post('/send', function (req, res) {
const myData = new Todo(req.body);
myData.save()
.then(() => {
message = true;
res.redirect('/');
})
.catch(err => {
res.status(400).send("unable to save to database");
});
});
// render a updated to do page
app.get('/edit/:id', (req, res) => {
const id = req.params.id;
Todo.find({}, (err, tasks) => {
if(err) res.send('error');
res.render(__dirname + '/index.html', { Todo: tasks, idTask: id ,message });
});
});
app.post('/edit/:id' ,(req , res) => {
Todo.updateOne({_id: req.params.id},
{
name: req.body.name,
priority : req.body.priority
},
(err, tasks) => {
if(err) res.send('error cannot modify');
else{
message = true;
res.redirect('/');
}
});
})
// delete a todo item
app.get('/remove/:id', function(req, res){
Todo.deleteOne({_id: req.params.id},
(err, tasks) => {
if(err) throw err;
else{
message = true;
res.redirect('/');
}
});
});