-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
205 lines (185 loc) · 5.34 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const methodOverride = require("method-override");
const bodyParser = require("body-parser");
const cors = require("cors");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const MongoStore = require("connect-mongo");
const flash = require("connect-flash");
const passport = require("passport");
const LocalStrategy = require("passport-local");
const Listing = require("./models/listing");
const User = require("./models/user");
dotenv.config();
const MONGO_URL = process.env.ATLASDB_URL;
console.log("MongoDB URI:", MONGO_URL);
async function connectToDatabase() {
try {
await mongoose.connect(MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("Connected to DB");
} catch (err) {
console.error("Database connection error:", err.message);
}
}
connectToDatabase();
const sessionOptions = {
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: true,
store: MongoStore.create({
mongoUrl: MONGO_URL,
collectionName: "sessions",
ttl: 14 * 24 * 60 * 60,
}),
cookie: {
expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7), // 7 days
maxAge: 1000 * 60 * 60 * 24 * 7,
httpOnly: true,
},
};
// Middleware setup
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(methodOverride("_method"));
app.use(cookieParser());
app.use(session(sessionOptions));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
// Passport configuration
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
// Routes
app.get("/", (req, res) => {
res.json({ message: "Welcome to List Your Business API" });
});
app.get("/signup", (req, res) => {
res.json({ message: "Signup page" });
});
app.post("/signup", async (req, res) => {
try {
const { username, email, password } = req.body;
const user = new User({ username, email });
const registeredUser = await User.register(user, password);
res.json(registeredUser);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.get("/login", (req, res) => {
res.json({ message: "Login page" });
});
app.post(
"/login",
passport.authenticate("local", {
failureRedirect: "/login",
failureFlash: true,
}),
(req, res) => {
res.json({ message: "Logged in" });
}
);
// CRUD routes for listings
app.get("/api/listings", async (req, res) => {
try {
const allListings = await Listing.find({});
res.json(allListings);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.get("/api/listings/:id", async (req, res) => {
try {
const { id } = req.params;
const listing = await Listing.findById(id);
if (!listing) {
return res.status(404).json({ error: "Listing not found" });
}
res.json(listing);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.post("/api/listings", async (req, res) => {
try {
const { title, description, image, location, country } = req.body;
if (!title || !description || !image || !location || !country) {
return res.status(400).json({ error: "All fields are required" });
}
const newListing = new Listing({
title,
description,
image,
location,
country,
});
await newListing.save();
res.status(201).json(newListing);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.put("/api/listings/:id", async (req, res) => {
try {
const { id } = req.params;
const { title, description, image, location, country } = req.body;
const updatedListing = await Listing.findByIdAndUpdate(
id,
{ title, description, image, location, country },
{ new: true }
);
if (!updatedListing) {
return res.status(404).json({ error: "Listing not found" });
}
res.json(updatedListing);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.delete("/api/listings/:id", async (req, res) => {
try {
const { id } = req.params;
const deletedListing = await Listing.findByIdAndDelete(id);
if (!deletedListing) {
return res.status(404).json({ error: "Listing not found" });
}
res.json({ message: "Listing deleted successfully" });
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.post("/api/listings/:id/reviews", async (req, res) => {
try {
const { id } = req.params;
const { text } = req.body;
const listing = await Listing.findById(id);
if (!listing) {
return res.status(404).json({ error: "Listing not found" });
}
listing.reviews.push({ text });
await listing.save();
res.json(listing);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal Server Error" });
}
});
// Start the server
const PORT = 8080;
app.listen(PORT, () => {
console.log(`Backend API running on port ${PORT}`);
});