-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
90 lines (72 loc) · 2.29 KB
/
app.js
File metadata and controls
90 lines (72 loc) · 2.29 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
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
const express = require('express');
const async = require('hbs/lib/async');
const path= require('path');
const { findOneAndUpdate, findByIdAndUpdate } = require('../toggle/models/toddle');
const Toddle = require("../toggle/models/toddle");
require("../toggle/connection/conn");
const port = process.env.PORT || 8000;
const app = express();
app.use(express.json())
app.use(express.urlencoded({extended:false}))
const staticPath = path.join(__dirname, '/public')
app.use(express.static(staticPath))
// console.log(staticPath);
app.set("view engine", "ejs")
app.get('/toddle', async(req,res)=>{
try {
await Toddle.find((err, docs)=>{
res.render('index', {
data : docs
})
})
} catch (error) {
console.log(error)
}
})
app.get('/edit/:id',async(req,res)=>{
// const _id = req.params.id
await Toddle.findOneAndUpdate({_id : req.params.id},req.body,{new:true}, (err, data)=>{
if (err) {
console.log('Error handelling');
}else{
res.render('edit', {Toddle:data})
}
})
})
app.post('/toddle', async(req,res)=>{
try {
// console.log(req.body.name)
const toddleData = new Toddle({
name: req.body.name,
profession: req.body.profession,
age: req.body.age
})
const data = await toddleData.save()
res.redirect('/toddle')
} catch (error) {
console.log(`${error}`);
}
})
app.post('/edit/:id', async(req,res)=>{
const _id = req.params.id
await Toddle.findByIdAndUpdate(_id, req.body, {new: true}, (err,data)=>{
if (err){
console.log("Error in updating data");
}else {
res.redirect('/toddle')
}
})
})
app.get('/delete/:id', async(req,res)=>{
const _id = req.params.id
await Toddle.findByIdAndDelete({_id}, (err,data)=>{
if (err){
console.log("Error in deleting data");
}else {
res.redirect('/toddle')
}
})
})
app.listen(port, ()=>{
console.log(`Listening to the port ${port}`);
})