-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
156 lines (135 loc) · 3.68 KB
/
app.js
File metadata and controls
156 lines (135 loc) · 3.68 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
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
147
148
149
150
151
152
153
154
155
156
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const { Types } = require("mongoose");
const Listing = require("./models/listing.js");
const path = require("path");
const MONGO_URL = "mongodb://localhost:27017/wonderlust";
main()
.then(() => console.log("Mongoose is connected"))
.catch(err => console.error("Problem connecting to database:", err));
async function main() {
try {
await mongoose.connect(MONGO_URL);
console.log("Connected to MongoDB successfully");
} catch (err) {
console.error("MongoDB connection error:", err);
process.exit(1);
}
}
app.set("view engine" , "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.urlencoded({extended:true}));
app.use(express.json()); // Add this for JSON parsing
// ===== API ROUTES (for Postman) =====
// Get all listings as JSON
app.get("/api/listings", async (req, res) => {
try {
const listings = await Listing.find({});
res.json({
success: true,
count: listings.length,
data: listings
});
} catch (err) {
console.error(err);
res.status(500).json({
success: false,
error: "Error fetching listings"
});
}
});
// Get single listing as JSON
app.get("/api/listings/:id", async (req, res) => {
try {
let { id } = req.params;
if (!Types.ObjectId.isValid(id)) {
return res.status(400).json({
success: false,
error: "Invalid listing ID format"
});
}
const listing = await Listing.findById(id);
if (!listing) {
return res.status(404).json({
success: false,
error: "Listing not found"
});
}
res.json({
success: true,
data: listing
});
} catch (err) {
console.error("Error fetching listing:", err);
res.status(500).json({
success: false,
error: "Server error"
});
}
});
// Create new listing (POST)
app.post("/api/listings", async (req, res) => {
try {
const newListing = new Listing(req.body);
await newListing.save();
res.status(201).json({
success: true,
data: newListing
});
} catch (err) {
console.error(err);
res.status(500).json({
success: false,
error: "Error creating listing"
});
}
});
// ===== WEB ROUTES (for browser) =====
app.get("/", (req, res) => {
res.send("Hi, I'm the root");
});
//index route
app.get("/listings", async (req, res) => {
try {
const listings = await Listing.find({});
res.render("listings/index.ejs", {listings}); // send back all listings
} catch (err) {
console.error(err);
res.status(500).send("Error fetching listings");
}
});
//show route - Fixed to include :id parameter
app.get("/listings/:id", async (req, res) => {
try {
let { id } = req.params;
if (!Types.ObjectId.isValid(id)) {
return res.status(400).send("Invalid listing ID format");
}
const listing = await Listing.findById(id);
if (!listing) {
return res.status(404).send("Listing not found in MongoDB");
}
// render show.ejs
res.render("listings/show", { listing });
} catch (err) {
console.error("Error fetching listing:", err);
res.status(500).send("Server error");
}
});
// For testing DB insert
// app.get("/testlisting", async (req, res) => {
// let sampleListing = new Listing({
// title: "my new home",
// description: "by the beach",
// price: 1200,
// location: "pune",
// country: "india",
// });
// await sampleListing.save();
// console.log("Sample was saved");
// res.send("Successful testing");
// });
app.listen(8080, () => {
console.log("Server is started on port 8080");
});