-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
115 lines (98 loc) Β· 3.18 KB
/
app.js
File metadata and controls
115 lines (98 loc) Β· 3.18 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
112
113
114
115
const express = require("express");
const app = 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 wrapAsync = require("./utils/wrapAsync.js");
const { listingSchema } = require("./schema.js");
const Review = require("./models/review.js");
const MONGO_URL = "mongodb://127.0.0.1:27017/wanderlust1";
// Database connection
main()
.then(() => {
console.log("Connected to DB");
})
.catch((err) => {
console.log("DB Connection Error: ", err);
});
async function main() {
await mongoose.connect(MONGO_URL);
}
// Set up view engine
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);
// Serve static files (styles, images, etc.)
app.use(express.static(path.join(__dirname, "public")));
// Import home route
const homeRoutes = require("./routes/home");
app.use("/", homeRoutes); // Use home route for the root path
// Listings Routes
app.get("/listings", async (req, res) => {
const allListings = await Listing.find({});
res.render("listings/index", { allListings });
});
app.get("/listings/new", (req, res) => {
res.render("listings/new");
});
app.get("/listings/:id", async (req, res) => {
let { id } = req.params;
const listing = await Listing.findById(id).populate("reviews");
res.render("listings/show", { listing });
});
app.post("/listings", async (req, res, next) => {
try {
if (!req.body.listing) {
throw new Error("No listing data provided in the request body.");
}
const newListing = new Listing(req.body.listing);
await newListing.save();
res.redirect("/listings");
} catch (err) {
console.error("Error saving listing:", err);
next(err);
}
});
app.get("/listings/:id/edit", async (req, res) => {
let { id } = req.params;
const listing = await Listing.findById(id);
res.render("listings/edit", { listing });
});
app.put("/listings/:id", async (req, res) => {
let { id } = req.params;
await Listing.findByIdAndUpdate(id, { ...req.body.listing });
res.redirect(`/listings/${id}`);
});
app.delete("/listings/:id", async (req, res) => {
let { id } = req.params;
await Listing.findByIdAndDelete(id);
res.redirect("/listings");
});
// Reviews Routes
app.post("/listings/:id/reviews", async (req, res) => {
let listing = await Listing.findById(req.params.id);
let newReview = new Review(req.body.review);
listing.reviews.push(newReview);
await newReview.save();
await listing.save();
res.redirect(`/listings/${listing._id}`);
});
app.delete("/listings/:id/reviews/:reviewId", wrapAsync(async (req, res) => {
const { id, reviewId } = req.params;
await Listing.findByIdAndUpdate(id, { $pull: { reviews: reviewId } });
await Review.findByIdAndDelete(reviewId);
res.redirect(`/listings/${id}`);
}));
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err);
res.status(500).send("Something went wrong");
});
// Start server
app.listen(3003, () => {
console.log("Server is running on port 3003");
});