-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
63 lines (58 loc) · 1.56 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
// See https://github.com/typicode/json-server#module
const jsonServer = require("json-server");
const server = jsonServer.create();
const router = jsonServer.router("db.json");
const middlewares = jsonServer.defaults();
const db = require("./db.json");
const PORT = 8001;
server.use(jsonServer.bodyParser); //necessary to parse req body
server.use(middlewares);
// Add this before server.use(router)
server.use(
jsonServer.rewriter({
"/api/*": "/$1",
"/blog/:resource/:id/show": "/:resource/:id",
})
);
// server.get("/tags", function (req, res) {
// const allTags = [];
// const items = db.items;
// items.forEach((item) => {
// item.tags.forEach((tag) => {
// if (allTags.find((item) => item.name === tag)) return;
// allTags.push({ name: tag, slug: tag });
// });
// });
// return res.jsonp(allTags);
// });
server.post("/login", function (req, res) {
const { email, password } = req.body;
if (!email || !password) {
return res.jsonp({
code: "error",
message: "credentials required",
});
}
const users = db.users;
const user = users.filter((user) => user.email === email)[0];
if (!user)
return res.jsonp({
code: "error",
message: "user not exist",
});
if (user.password !== password)
return res.jsonp({
code: "error",
message: "invalid credentials",
});
return res.jsonp({
success: true,
data: user,
});
});
server.use(router);
server.listen(PORT, () => {
console.log(`JSON Server is running port: ${PORT}`);
});
// Export the Server API
module.exports = server;