forked from Technigo/js-project-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
234 lines (213 loc) · 5.9 KB
/
server.js
File metadata and controls
234 lines (213 loc) · 5.9 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import express from "express";
import listEndpoints from "express-list-endpoints";
import cors from "cors";
import mongoose from "mongoose";
import crypto from "crypto";
import bcrypt from "bcrypt";
// Setup
const port = process.env.PORT || 8080;
const app = express();
const mongoURL = process.env.mongoURL || "mongodb://127.0.0.1/happy-thoughts";
mongoose.connect(mongoURL);
mongoose.Promise = Promise;
app.use(cors());
app.use(express.json());
// Schemas
const UserSchema = new mongoose.Schema({
email: {
type: String,
required: [true, "Email is required"],
unique: true,
match: [/.+@.+\..+/, "Invalid email format"],
},
password: {
type: String,
required: true,
minlength: 6,
},
accessToken: {
type: String,
default: () => crypto.randomBytes(128).toString("hex"),
},
});
const ThoughtSchema = new mongoose.Schema({
message: {
type: String,
required: [true, "Message is required"],
minlength: 5,
maxlength: 140,
},
hearts: {
type: Number,
default: 0,
},
createdAt: {
type: Date,
default: Date.now,
},
createdBy: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
default: null,
},
});
const User = mongoose.model("User", UserSchema);
const Thought = mongoose.model("Thought", ThoughtSchema);
// Auth middleware
const authenticateUser = async (req, res, next) => {
const accessToken = req.header("Authorization");
try {
const user = await User.findOne({ accessToken });
if (user) {
req.user = user;
next();
} else {
res.status(401).json({ error: "Please log in to access this resource" });
}
} catch {
res.status(401).json({ error: "Invalid request" });
}
};
// Routes
app.get("/", (req, res) => {
res.json({
message: "Welcome to Oscar's Thoughts API!",
endpoints: listEndpoints(app),
});
});
app.get("/thoughts", async (req, res) => {
const { page = 1, limit = 5 } = req.query;
try {
const totalThoughts = await Thought.countDocuments();
const thoughts = await Thought.find()
.sort({ createdAt: -1 })
.skip((page - 1) * limit)
.limit(Number(limit));
res.json({
page: Number(page),
totalThoughts,
totalPages: Math.ceil(totalThoughts / limit),
results: thoughts,
});
} catch {
res.status(500).json({ error: "Could not fetch thoughts" });
}
});
app.get("/thoughts/:id", async (req, res) => {
try {
const thought = await Thought.findById(req.params.id);
if (!thought) return res.status(404).json({ error: "Thought not found" });
res.json(thought);
} catch {
res.status(400).json({ error: "Invalid ID" });
}
});
app.post("/thoughts", async (req, res) => {
const { message } = req.body;
const accessToken = req.header("Authorization");
try {
let createdBy = null;
if (accessToken) {
const user = await User.findOne({ accessToken });
if (user) {
createdBy = user._id;
}
}
const newThought = new Thought({ message, createdBy });
const savedThought = await newThought.save();
res.status(201).json(savedThought);
} catch (err) {
res.status(400).json({ error: err.message });
}
});
app.post("/thoughts/:id/like", async (req, res) => {
try {
const updated = await Thought.findByIdAndUpdate(
req.params.id,
{ $inc: { hearts: 1 } },
{ new: true }
);
if (!updated) return res.status(404).json({ error: "Thought not found" });
res.status(200).json(updated);
} catch {
res.status(400).json({ error: "Invalid ID" });
}
});
app.patch("/thoughts/:id", authenticateUser, async (req, res) => {
try {
const thought = await Thought.findById(req.params.id);
if (!thought) return res.status(404).json({ error: "Thought not found" });
if (
!thought.createdBy ||
thought.createdBy.toString() !== req.user._id.toString()
) {
return res
.status(403)
.json({ error: "Not allowed to edit this thought" });
}
thought.message = req.body.message;
await thought.save();
res.json(thought);
} catch (err) {
res.status(400).json({ error: err.message });
}
});
app.delete("/thoughts/:id", authenticateUser, async (req, res) => {
try {
const thought = await Thought.findById(req.params.id);
if (!thought) return res.status(404).json({ error: "Thought not found" });
if (
!thought.createdBy ||
thought.createdBy.toString() !== req.user._id.toString()
) {
return res
.status(403)
.json({ error: "Not allowed to delete this thought" });
}
await thought.deleteOne();
res.status(204).end();
} catch {
res.status(400).json({ error: "Invalid ID" });
}
});
app.post("/register", async (req, res) => {
const { email, password } = req.body;
try {
if (!email || !password)
return res.status(400).json({ error: "All fields are required" });
const existing = await User.findOne({ email });
if (existing)
return res
.status(400)
.json({ error: "That email address already exists" });
const hashed = bcrypt.hashSync(password, bcrypt.genSaltSync());
const newUser = await new User({ email, password: hashed }).save();
res.status(201).json({
email: newUser.email,
id: newUser._id,
accessToken: newUser.accessToken,
});
} catch (err) {
res.status(400).json({ error: err.message });
}
});
app.post("/login", async (req, res) => {
const { email, password } = req.body;
try {
const user = await User.findOne({ email });
if (!user || !bcrypt.compareSync(password, user.password)) {
return res.status(401).json({ error: "Invalid email or password" });
}
res.status(200).json({
email: user.email,
id: user._id,
accessToken: user.accessToken,
});
} catch {
res.status(400).json({ error: "Something went wrong" });
}
});
// Server startup
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});