This repository has been archived by the owner on Jul 25, 2021. It is now read-only.
generated from mhstrkmp/awesome-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
142 lines (131 loc) · 3.62 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
require("dotenv").config();
const express = require("express");
const path = require("path");
const { dbConnect } = require("./lib/database");
const {
createObject,
getObjects,
getUserById,
updateUserById,
getItemsByCategory,
getItemById,
authenticateUser,
} = require("./lib/api");
const app = express();
app.use(express.json());
const port = process.env.PORT || 3600;
const allowedCollections = ["users", "members", "items", "orders", "customers"];
allowedCollections.forEach((collection) => {
app
.route(`/api/${collection}`)
.get(async (req, res) => {
try {
const collectionObjects = await getObjects(`${collection}`);
res.send(collectionObjects);
} catch (error) {
console.error(error);
res
.status(500)
.send("An unexpected error occured. Please try again later!");
}
})
.post(async (req, res) => {
try {
await createObject(req, collection);
res.send(`Successfully updated collection: ${collection}`);
} catch (error) {
console.error(error);
res
.status(500)
.send("An unexpected error occured. Please try again later!");
}
});
});
app
.route("/api/users/:id")
.get(async (req, res) => {
const { id } = req.params;
try {
const users = await getUserById(id);
res.send(users);
} catch (error) {
console.error(error);
res
.status(500)
.send("An unexpected error occured. Please try again later!");
}
})
.put(async (req, res) => {
const { id } = req.params;
try {
await updateUserById(req.body, id);
res.send(`Successfully updated userId: ${id}`);
} catch (error) {
console.error(error);
res
.status(500)
.send("An unexpected error occured. Please try again later!");
}
});
app.route("/api/category/:id").get(async (req, res) => {
const { id } = req.params;
try {
const users = await getItemsByCategory(id);
res.send(users);
} catch (error) {
console.error(error);
res
.status(500)
.send("An unexpected error occured. Please try again later!");
}
});
app.route("/api/items/:id").get(async (req, res) => {
const { id } = req.params;
try {
const users = await getItemById(id);
res.send(users);
} catch (error) {
console.error(error);
res
.status(500)
.send("An unexpected error occured. Please try again later!");
}
});
app.route("/api/auth/:username/:password").get(async (req, res) => {
// Send username, password
const { username, password } = req.params;
try {
// Search for corresponding user
const userData = await authenticateUser("users", username, password);
res.send(userData);
} catch (error) {
console.error(error);
res
// Error message: Wrong username or password please try again
.status(500)
.send("An unexpected error occured. Please try again later!");
}
});
app.use(express.static("public"));
app.use(express.static(path.join(__dirname, "client/build")));
app.use(
"/storybook",
express.static(path.join(__dirname, "client/storybook-static"))
);
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client/build", "index.html"));
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});
async function runDatabase() {
try {
await dbConnect(process.env.DB_URI, process.env.DB_NAME);
console.log("Connecting to database ...");
console.log("Connected to database");
console.log(`API listening at http://localhost:${port}`);
} catch (error) {
console.error(error);
}
}
runDatabase();