-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathserver.js
64 lines (53 loc) · 1.36 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
const jsonServer = require("json-server");
const server = jsonServer.create();
const router = jsonServer.router("db.json");
const db = require("./db.json");
const middlewares = jsonServer.defaults();
const port = 3005;
server.use(middlewares);
server.use(jsonServer.bodyParser);
server.get("*", (_, res) => {
res.set("Allow", "POST");
res.send(405, "Method Not Allowed");
});
server.post("/customer/account/resetPassword", resetController);
server.use(router);
server.listen(port, () => {
console.log(`JSON Server is running on port ${port}`);
});
async function resetController(req, res, next) {
try {
const userId = req.body.email;
if (!userId) {
return res.status(400).jsonp({
data: null,
errors: [
{
status: "400",
title: "email field not specified",
detail: "An email address is required",
},
],
});
}
const result = db.users.find((user) => user.email == userId);
if (!result) {
return res.status(404).jsonp({
data: null,
errors: [
{
status: "404",
title: "Unknown user",
detail: "Sorry, there is no account attached to that email address",
},
],
});
}
res.status(200).jsonp({
data: null,
errors: [],
});
} catch (e) {
next();
}
}