-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (48 loc) · 1.38 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
// importing the dependencies
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const Todo = require('./models/todo');
// declaring the port
const port = 7000;
// setting the view engine
app.set('view engine', 'ejs')
// using the static css page
app.use(express.static('public'))
// using bodyparser
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
// Connecting the database
const dburl = 'mongodb://127.0.0.1:27017/tododb'
mongoose.connect(dburl, { useNewUrlParser: true, useUnifiedTopology: true });
// getting the data and rendering it
app.get('/', (req, res) => {
Todo.find()
.then(result => {
res.render('index', { data: result });
console.log(result);
})
// res.render('index');
});
// posting the data for the user to see
app.post('/', (req, res) => {
const todo = new Todo({
todo: req.body.todoValue
})
todo.save()
.then(result => {
res.redirect('back')
})
});
// deleting a particular task by id from mongodb collection
app.delete('/:id', (req, res) => {
Todo.findByIdAndDelete(req.params.id)
.then(result => {
console.log(result);
});
});
// listner
app.listen(port, () => {
console.log('server is up and running on port', port);
});