Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
204 lines (175 loc) · 5.17 KB
/
Copy pathserver.js
File metadata and controls
204 lines (175 loc) · 5.17 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
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
const dotenv = require("dotenv");
const envFile = `.env.${process.env.NODE_ENV || "development"}`;
dotenv.config({ path: envFile });
const express = require("express");
const cors = require("cors");
const connectDB = require("./db");
const Post = require("./models/Post");
const authRoutes = require("./routes/auth");
const auth = require("./middleware/auth");
const {
validatePost,
validatePostUpdate,
handleValidationErrors,
} = require("./middleware/validation");
const rateLimitMiddleware = require("./middleware/rateLimit");
const asyncHandler = require("./middleware/asyncHandler");
const { errorHandler } = require("./middleware/errorHandler");
const { AppError } = require("./middleware/errorHandler");
const logger = require("./logger");
const postsRoutes = require("./routes/posts");
const app = express();
// CORS
app.use(
cors({
origin: "http://lcoalhost:5173",
credentials: true,
}),
);
const { specs, swaggerUi } = require("./swagger");
// Swagger documentation
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(specs));
app.use("/uploads", express.static("uploads"));
// ADD THESE 2 LINES (after app = express())
app.use(express.json()); // Parse JSON data
app.use(express.urlencoded({ extended: true })); // Parse form data
// Hardcoded data (top of file, after middleware)
const posts = [];
app.use("/api/auth", authRoutes);
app.use("/api/posts", postsRoutes);
// POST - Create
app.post(
"/api/posts",
auth,
validatePost,
handleValidationErrors,
asyncHandler(async (req, res) => {
const { title, author, content } = req.body;
const newPost = await Post.create({
title,
author,
content,
user: req.userId,
});
res
.status(201)
.json({ success: true, message: "Post created", post: newPost });
}),
);
// GET - User's posts
app.get(
"/api/posts/my/posts",
auth,
asyncHandler(async (req, res) => {
const posts = await Post.find({ user: req.userId })
.populate("user", "username email")
.sort({ createdAt: -1 });
res.json({ success: true, count: posts.length, posts });
}),
);
// GET - All posts (pagination)
app.get(
"/api/posts",
asyncHandler(async (req, res) => {
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 10;
const skip = (page - 1) * limit;
let query = {};
if (req.query.search) {
query.$or = [
{ title: { $regex: req.query.search, $options: "i" } },
{ content: { $regex: req.query.search, $options: "i" } },
];
}
if (req.query.author) {
query.author = { $regex: req.query.author, $options: "i" };
}
let sortOption = { createdAt: -1 };
if (req.query.sortBy) {
const sortField = req.query.sortBy;
const sortOrder = req.query.order === "asc" ? 1 : -1;
sortOption = { [sortField]: sortOrder };
}
const total = await Post.countDocuments(query);
const posts = await Post.find(query)
.populate("user", "username email")
.skip(skip)
.limit(limit)
.sort(sortOption);
res.json({
success: true,
page,
limit,
total,
totalPages: Math.ceil(total / limit),
count: posts.length,
posts,
});
}),
);
// GET - Single post
app.get(
"/api/posts/:id",
asyncHandler(async (req, res) => {
const post = await Post.findById(req.params.id).populate(
"user",
"username email",
);
if (!post) throw new AppError("Post not found", 404);
res.json({ success: true, post });
}),
);
// PUT - Update
app.put(
"/api/posts/:id",
auth,
validatePostUpdate,
handleValidationErrors,
asyncHandler(async (req, res) => {
const { title, author, content } = req.body;
const post = await Post.findById(req.params.id);
if (!post) throw new AppError("Post not found", 404);
if (post.user.toString() !== req.userId) {
throw new AppError("Forbidden: You can only edit your own posts", 403);
}
const updateData = {};
if (title) updateData.title = title;
if (author) updateData.author = author;
if (content) updateData.content = content;
const updatedPost = await Post.findByIdAndUpdate(
req.params.id,
updateData,
{ new: true, runValidators: true },
);
res.json({ success: true, message: "Post updated", post: updatedPost });
}),
);
// DELETE
app.delete(
"/api/posts/:id",
auth,
asyncHandler(async (req, res) => {
const post = await Post.findById(req.params.id);
if (!post) throw new AppError("Post not found", 404);
if (post.user.toString() !== req.userId) {
throw new AppError("Forbidden: You can only delete your own posts", 403);
}
await Post.findByIdAndDelete(req.params.id);
res.json({ success: true, message: "Post deleted" });
}),
);
// Error middleware - LAST!
app.use(errorHandler);
// Only listen if not in test mode
if (process.env.NODE_ENV !== "test") {
connectDB().then(() => {
app.listen(process.env.PORT || 3000, () => {
logger.info(`Server running on port ${process.env.PORT || 3000}`);
console.log(
`Server running on http://localhost:${process.env.PORT || 3000}`,
);
});
});
}
module.exports = app;
module.exports = app; // Export for testing