add migrations to start up
This commit is contained in:
parent
099ffb74a1
commit
85cd083750
1 changed files with 28 additions and 15 deletions
31
src/index.ts
31
src/index.ts
|
|
@ -13,22 +13,35 @@ const app = express();
|
|||
app.use(express.json());
|
||||
app.use(errorHandler);
|
||||
|
||||
const PORT = process.env.PORT ? parseInt(process.env.PORT) : 4000;
|
||||
const HOST = process.env.HOST || "localhost";
|
||||
|
||||
async function startServer() {
|
||||
try {
|
||||
// 1️⃣ Initialize database
|
||||
await AppDataSource.initialize();
|
||||
console.log("Data Source initialized");
|
||||
|
||||
// Optional: run pending migrations
|
||||
await AppDataSource.runMigrations();
|
||||
console.log("Migrations executed");
|
||||
|
||||
// 2️⃣ Setup routes
|
||||
app.use("/auth", authRoutes);
|
||||
app.use("/user", userRoutes);
|
||||
//app.use("/recipe", recipePoint);
|
||||
|
||||
app.get("*", (req: Request, res: Response) => {
|
||||
res.status(505).json({ message: "Bad Request" });
|
||||
});
|
||||
|
||||
AppDataSource.initialize()
|
||||
.then(async () => {
|
||||
// 3️⃣ Start listening
|
||||
const PORT = parseInt(process.env.PORT || "4000", 10);
|
||||
const HOST = process.env.HOST || "localhost";
|
||||
app.listen(PORT, HOST, () => {
|
||||
console.log(`Server is running on http://${HOST}:${PORT}`);
|
||||
console.log(`Server running on http://${HOST}:${PORT}`);
|
||||
});
|
||||
console.log("Data Source has been initialized!");
|
||||
})
|
||||
.catch((error) => console.log(error));
|
||||
} catch (err) {
|
||||
console.error("Error during Data Source initialization:", err);
|
||||
}
|
||||
}
|
||||
|
||||
// Call the async start function
|
||||
startServer();
|
||||
Loading…
Add table
Add a link
Reference in a new issue