feat: basic db connection
This commit is contained in:
@@ -2,6 +2,7 @@ import express, { Express, Request, Response } from "express";
|
||||
import cors from "cors";
|
||||
import * as dotenv from "dotenv";
|
||||
import transactionsRouter from './routes/transactions';
|
||||
import { db, testConnection } from "./db";
|
||||
|
||||
|
||||
dotenv.config();
|
||||
@@ -17,6 +18,19 @@ app.get("/api/health", (req: Request, res: Response) => {
|
||||
app.use('/api/transactions', transactionsRouter);
|
||||
|
||||
const PORT: number = parseInt(process.env.PORT as string) || 3000;
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server running on port ${PORT}`);
|
||||
|
||||
// Start server after DB connection is established
|
||||
async function startServer() {
|
||||
await testConnection(); // Test DB connection first
|
||||
|
||||
// Sync models (use migrations in production!)
|
||||
await db.sync({ alter: true }); // Use { force: true } to drop and recreate tables (development only!)
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`🚀 Backend Server running on http://localhost:${PORT}`);
|
||||
});
|
||||
}
|
||||
startServer().catch((err) => {
|
||||
console.error('Failed to start server:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user