-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (107 loc) · 3.03 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const express = require("express");
const app=express();
const path = require("path");
const mongoose =require("mongoose");
const chat = require("./models/chat.js");
const methodOvverride=require("method-override");
const ExpressError=require("./ExpressError.js");
let port=2125;
app.set("views",path.join(__dirname,"Views"));
app.set("view engine","ejs");
app.use(express.static(path.join(__dirname,"public")));
app.use(express.urlencoded({extended:true}));
app.use(methodOvverride("_method"));
main().then(()=>{
console.log("connection successful");
}).catch(err => console.log(err));
async function main() {
await mongoose.connect('mongodb://127.0.0.1:27017/fakewhatsapp');
}
app.get("/",(req,res)=>{
res.send("app is working");
});
app.get("/chat",async(req,res)=>{
let chats=await chat.find();
res.render("index.ejs",{chats});
});
// New route
app.get("/chat/new",(req,res)=>{
// throw new ExpressError(401,"page not found");
// res.send("work well");
res.render("newchat.ejs")
});
// create route
app.post("/chat",async(req,res,next)=>{
try {
let {from , to ,msg}=req.body;
console.log(from,to,msg);
let newChat= new chat({
from:from,
to:to,
msg:msg,
created_at:new Date(),
});
await newChat.save();
res.redirect("/chat");
} catch (er) {
next(er);
}
});
function asyncWrap(fn){
return function(req,res,next){
fn(req,res,next).catch((err)=>
next(err)
);
}
}
// New - Show Route
app.get("/chat/:id",asyncWrap( async(req,res,next)=>{
let {id}=req.params;
let data=await chat.findById(id);
if(!data)
{
next(new ExpressError(402,"chat not found"));
}
res.render("edit.ejs",{data});
console.log("show route");
}));
// Edit Route
app.get("/chat/:id/edit",asyncWrap(async(req,res,next)=>{
let {id} = req.params;
// console.log(id)
let data = await chat.findById(id);
// console.log(data);
res.render("edit.ejs",{data});
}));
// update route
app.put("/chat/:id",asyncWrap(async(req,res,next)=>{
let {id}=req.params;
let {newMsg}=req.body;
console.log(newMsg);
let updateChat=await chat.findByIdAndUpdate(id,{msg:newMsg},{runValidators:true,new: true});
console.log(updateChat);
res.redirect("/chat");
}));
app.delete("/chat/:id", async(req,res)=>{
console.log("work");
let {id}=req.params;
console.log(id);
let deletedChat=await chat.findByIdAndDelete(id);
console.log(deletedChat);
res.redirect("/chat");
})
app.get("/chat/:id/delete",async(req,res)=>{
let {id}=req.params;
console.log(id);
let chats=await chat.findById(id);
res.render("delete.ejs",{chats});
console.log(chats);
});
// error handling middleware
app.use((er,req,res,next)=>{
let {status=500,message="some error occured"}=er;
res.status(status).send(message);
})
app.listen(port,()=>{
console.log("app is litening on port: ",port);
})