42 lines
933 B
JavaScript
42 lines
933 B
JavaScript
|
const express = require("express");
|
||
|
const cors = require("cors");
|
||
|
const { PrismaClient } = require("@prisma/client");
|
||
|
const appRoutes = require("./routes/appRoutes");
|
||
|
|
||
|
const app = express();
|
||
|
const port = process.env.PORT || 3000;
|
||
|
const prisma = new PrismaClient();
|
||
|
|
||
|
function setupMiddleware(app) {
|
||
|
app.use(cors());
|
||
|
app.use(express.json());
|
||
|
app.use("/api", appRoutes);
|
||
|
}
|
||
|
|
||
|
setupMiddleware(app);
|
||
|
|
||
|
// Start server
|
||
|
async function startServer() {
|
||
|
try {
|
||
|
app.listen(port);
|
||
|
console.log(`Server is running on http://localhost:${port}`);
|
||
|
} catch (error) {
|
||
|
console.error("Error starting the server:", error);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
process.on("SIGINT", async () => {
|
||
|
try {
|
||
|
await prisma.$disconnect();
|
||
|
console.log("Prisma client disconnected");
|
||
|
process.exit(0);
|
||
|
} catch (error) {
|
||
|
console.error("Error disconnecting Prisma client:", error);
|
||
|
process.exit(1);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
startServer();
|
||
|
|
||
|
module.exports = { app, prisma };
|