-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
111 lines (89 loc) · 2.92 KB
/
app.js
File metadata and controls
111 lines (89 loc) · 2.92 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const express = require("express");
const mongoose = require("mongoose");
const Listing = require("./models/listing.js");
const path = require("path");
const methodOverride = require("method-override");
const ejsMate = require("ejs-mate");
const asyncWrap = require("./utils/asyncWrap.js");
const ExpressError = require("./utils/ExpressError.js");
const {listingSchema} = require("./Schema.js");
const listingValidate = (req,res,next) => {
let {error} = listingSchema.validate(req.body);
console.log(error);
if(error){
let errMsg = error.details.map((el) => el.message).join(",")
throw new ExpressError(400,errMsg);
}
}
const MONGO_URL = "mongodb://127.0.0.1:27017/wanderlust";
const app = express();
app.set("view engine","ejs");
app.set("views",path.join(__dirname,"views"));
app.use(express.urlencoded({extended : true}));
app.use(methodOverride("_method"));
app.engine("ejs",ejsMate);
app.use(express.static(path.join(__dirname,"/public")));
main()
.then(() => {
console.log("Connected to Database");
})
.catch((err) => {
console.log(err);
})
async function main() {
await mongoose.connect(MONGO_URL);
}
app.get("/",(req,res) => {
res.send("Request received");
});
//Index route
app.get("/listings",asyncWrap(async(req,res) => {
const allListings = await Listing.find({})
res.render("listings/index.ejs",{allListings,page:"index-page"});
}));
//New route
app.get("/listings/new",(req,res) => {
res.render("listings/new.ejs",{page : "new-page"});
});
//Create route
app.post("/listings",listingValidate,asyncWrap(async(req,res,next) => {
let newListing = new Listing(req.body.listing);
await newListing.save()
res.redirect("/listings");
}))
//Edit route
app.get("/listings/:id/edit",asyncWrap(async(req,res) => {
let {id} = req.params;
let listing = await Listing.findById(id);
res.render("listings/edit.ejs",{listing,page : "edit-page"});
}))
//Update route
app.put("/listings/:id",listingValidate,asyncWrap(async(req,res) => {
let {id} = req.params;
await Listing.findByIdAndUpdate(id,{...req.body.listing});
res.redirect(`/listings/${id}`);
}))
//Show route
app.get("/listings/:id",asyncWrap(async(req,res) => {
let {id} = req.params;
let listing = await Listing.findById(id);
res.render("listings/show.ejs",{listing,page: "show-page"});
}))
//Delete route
app.delete("/listings/:id",asyncWrap(async(req,res) => {
let {id} = req.params;
let deletedListing = await Listing.findByIdAndDelete(id);
console.log(deletedListing);
res.redirect("/listings");
}))
app.use((req,res,next) => {
next(new ExpressError(404,"Page not found"));
})
//Error handling middleware
app.use((err,req,res,next) => {
let {statusCode = 500,message = "Some error occured!"} = err;
res.status(statusCode).render("error.ejs",{err});
})
app.listen(8080,() => {
console.log("App is listening");
});