generated from majazimnoch/express-api-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
253 lines (230 loc) · 6.13 KB
/
server.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import express from 'express';
import cors from 'cors';
import mongoose from 'mongoose';
import dotenv from 'dotenv';
import bcrypt from 'bcrypt';
import User from './Models/user-users';
import Horse from './Models/horse-horses';
dotenv.config();
const mongoUrl = process.env.MONGO_URL || 'mongodb://127.0.0.1/horsey';
mongoose.connect(mongoUrl, { dbName: 'users', useNewUrlParser: true, useUnifiedTopology: true });
mongoose.Promise = Promise;
const port = process.env.PORT || 8080;
const app = express();
// Add middlewares to enable cors and json body parsing
app.use(cors());
app.use(express.json());
const authenticateUser = async (req, res, next) => {
const accessToken = req.header("Authorization");
try {
const user = await User.findOne({accessToken: accessToken});
if (user) {
req.user = user;
next();
} else {
res.status(401).json({
success: false,
response: "Please log in"
})
}
} catch (e) {
res.status(500).json({
success: false,
response: e
});
}
};
// Register
app.post("/register", async (req, res) => {
try {
const salt = bcrypt.genSaltSync();
const newUser = await new User({
name: req.body.name,
email: req.body.email,
password: bcrypt.hashSync(req.body.password, salt)
}).save()
res.status(201).json({
success: true,
response: {
name: newUser.name,
id: newUser._id,
accessToken: newUser.accessToken
}
})
} catch (e) {
res.status(400).json({
success: false,
response: e
})
}
});
app.post("/login", async (req, res) => {
const { email, password } = req.body;
try {
const user = await User.findOne({email: email})
console.log(user)
if (user && bcrypt.compareSync(password, user.password)) {
res.status(200).json({
success: true,
response: {
name: user.name,
id: user._id,
accessToken: user.accessToken
}
});
} else {
res.status(400).json({
success: false,
response: "Credentials do not match"
});
}
} catch (e) {
res.status(500).json({
success: false,
response: e
})
}
});
app.post("/horses", authenticateUser, async (req, res) => {
const { horse } = req.body;
const accessToken = req.header("Authorization");
const user = await User.findOne({ accessToken: accessToken });
try {
const newHorse = await new Horse({
horse,
userId: user._id,
username: user.name,
}).save();
res.status(201).json({
success: true,
response: newHorse,
});
} catch (e) {
res.status(400).json({
success: false,
response: e,
});
}
});
// GET-routes
// Only logged in users can see
app.get("/secrets", authenticateUser);
app.get("/secrets", (req, res) => {
res.json({ user: req.user, secret: "welcome to Horsey!"});
});
// All users
app.get("/users", authenticateUser)
app.get("/users", async (req, res) => {
try {
const users = await User.find()
res.status(200).json({
success: true,
response: users
})
} catch (e) {
res.status(400).json({success: false, response: e});
}
});
// All horses
app.get("/horses", authenticateUser)
app.get("/horses", async (req, res) => {
try {
const horses = await Horse.find()
res.status(200).json({
success: true,
response: horses
})
} catch (e) {
res.status(400).json({success: false, response: e});
}
});
//Show data from a specific user by id
app.get("/users/:userId", authenticateUser)
app.get("/users/:userId", async (req, res) => {
const { userId } = req.params;
try {
const userData = await User.findById({_id: userId})
res.status(200).json({
success: true,
response: userData
})
} catch (e) {
res.status(400).json({success: false, response: e});
}
});
// Show data from a single horse based on id
app.get("/horses/:horseId", authenticateUser)
app.get("/horses/:horseId", async (req, res) => {
const { horseId } = req.params;
try {
const singleHorse = await Horse.find({ _id: horseId }).sort({createdAt: 'desc'})
res.status(200).json({
success: true,
response: singleHorse
})
} catch (e) {
res.status(400).json({success: false, response: e});
}
});
//Show horses from a specific user
app.get("/users/:userId/horses", authenticateUser)
app.get("/users/:userId/horses", async (req, res) => {
const { userId } = req.params;
try {
const usersHorses = await Horse.find({userId: userId}).sort({createdAt: 'desc'})
const user = await User.findById({_id: userId})
res.status(200).json({
success: true,
response: usersHorses, user
})
} catch (e) {
res.status(400).json({success: false, response: e});
}
});
//PUT and DELETE routes - PATCH is more complex to implement while PUT is more standardized
// Delete horse
app.delete("/horses/:horseId", async (req, res) => {
const { horseId } = req.params
try {
const horseToDelete = await Horse.findByIdAndRemove({_id: horseId})
res.status(200).json({
success: true,
response: "Horse deleted", horseToDelete
})
} catch (e) {
res.status(400).json({
success: false,
response: e
})
}
});
// PUT to edit a horse
// {new:true} ber mongoose att skicka den nya versionen av hästen (objektet) ist f den gamla
app.put("/horses/:horseId", async (req, res) => {
const { horseId } = req.params
Horse.findByIdAndUpdate(horseId, req.body, {new:true}).then((horse) => {
if (!horse){
return res.status(404).json({
success: false,
response: "Horse not found!"
})
}
return res.status(200).json({
success: true,
response: horse
})
}).catch((error) => {
res.status(500).send(error)
})
})
const factsData = require('./RandomFacts/facts.json');
// Define a route to retrieve a random fact
app.get('/random-fact', (req, res) => {
const randomIndex = Math.floor(Math.random() * factsData.facts.length);
const randomFact = factsData.facts[randomIndex];
res.json(randomFact);
});
// Start the server
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});