-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
93 lines (76 loc) · 2.83 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
const express = require("express");
const mongoose = require("mongoose");
const methodOverride = require("method-override");
const ejsMate = require("ejs-mate");
const path = require("path");
const CampGround = require("./models/campground");
const catchAsync = require("./utils/catchAsync")
const ExpressError = require("./utils/ExpressError")
const { campgroundSchema } = require("./schema/schemas")
const app = express();
mongoose.connect("mongodb://localhost:27017/yelp-camp");
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", () => {
console.log("database connected");
});
app.set("view engine", "ejs");
app.set("views"), path.join(__dirname, "views");
app.engine("ejs", ejsMate); //for partials
app.use(express.urlencoded({ extended: true })); //to parse body
app.use(methodOverride('_method')); //for custom form methods
//middleware to validate request body
const validateCampground = (req, res, next) => {
const { error } = campgroundSchema.validate(req.body);
if (error) {
const msg = error.details.map(el => el.message).join(",");
throw new ExpressError(msg, 400);
} else {
next();
}
}
app.get("/", (req, res) => {
res.render("home");
});
app.get("/campgrounds", catchAsync(async (req, res) => {
const campgrounds = await CampGround.find({});
res.render("campgrounds/index", { campgrounds });
}));
app.post("/campgrounds", validateCampground, catchAsync(async (req, res, next) => {
const campground = new CampGround(req.body.campground);
await campground.save();
res.redirect(`/campgrounds/${campground._id}`);
}));
app.get("/campgrounds/new", (req, res) => {
res.render("campgrounds/new");
});
app.get("/campgrounds/:id", catchAsync(async (req, res) => {
const campground = await CampGround.findById(req.params.id);
res.render("campgrounds/show", { campground });
}));
app.get("/campgrounds/:id/edit", catchAsync(async (req, res) => {
const campground = await CampGround.findById(req.params.id);
res.render("campgrounds/edit", { campground });
}));
app.put("/campgrounds/:id", validateCampground, catchAsync(async (req, res) => {
const { id } = req.params;
await CampGround.findByIdAndUpdate(id, { ...req.body.campground });
res.redirect(`/campgrounds/${id}`);
}));
app.delete("/campgrounds/:id", catchAsync(async (req, res) => {
const { id } = req.params;
await CampGround.findByIdAndDelete(id);
res.redirect("/campgrounds");
}));
app.all("*", (req, res, next) => {
next(new ExpressError("Page not found", 404));
})
//error handler
app.use((err, req, res, next) => {
const { statusCode = 500 } = err;
if (!err.message) err.message = "Something went wrong";
res.status(statusCode).render("error", { err });
})
app.listen(3000, () => {
console.log("Serving on port 3000");
});