-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.js
More file actions
59 lines (49 loc) · 1.51 KB
/
Copy pathServer.js
File metadata and controls
59 lines (49 loc) · 1.51 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
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const database = require("./database.js");
const apiRouter = require("./api/api.js");
function setupServer() {
const app = express();
const corsOptions = {
origin: "*", // Allowing all origins, you might want to restrict this to specific domains
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
};
app.use(cors(corsOptions));
app.use(bodyParser.json());
// Middleware for logging requests
app.use((req, res, next) => {
console.log(`Received ${req.method} request for ${req.url}`);
next();
});
// Use the API router
app.use("/api", apiRouter);
// Error handling middleware
app.use((err, req, res, next) => {
console.error("An error occurred:", err);
// Log the full error stack trace
console.error(err.stack);
// Respond with a more detailed error message
res.status(500).json({
error: "Internal Server Error",
message: err.message, // Include the error message
stack: err.stack, // Include the stack trace
});
});
return new Promise((resolve, reject) => {
database
.connect()
.then(() => {
const PORT = 5000;
const server = app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
resolve(server);
});
})
.catch((error) => {
console.error("Failed to start the server:", error);
reject(error);
});
});
}
module.exports = setupServer;