-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
45 lines (37 loc) · 1.08 KB
/
index.mjs
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
import express, { json, urlencoded } from "express";
const app = express();
import dotenv from "dotenv";
dotenv.config();
import apiRoutes from "./routes/apiRoutes.mjs";
import authRoutes from "./routes/authRoutes.mjs";
app.use(json());
app.use(urlencoded({ extended: true }));
app.get("/", (req, res) => {
res.send("Welcome to my server!");
});
app.use("/api", apiRoutes);
app.use("/auth", authRoutes);
app.use((req, res, next) => {
res.status(404).json({ message: "Route not found" });
});
app.use((err, req, res, next) => {
res.status(500).json({ message: "Internal Server Error" });
});
const startServer = (port) => {
const serverPort = port || process.env.PORT || 5000;
return new Promise((resolve, reject) => {
const server = app
.listen(serverPort, () => {
console.log(`Server is running on PORT ${serverPort}`);
resolve(server);
})
.on("error", reject);
});
};
if (process.env.NODE_ENV !== 'test') {
startServer().catch(err => {
console.error('Failed to start server:', err);
process.exit(1);
});
}
export { app, startServer };