-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
38 lines (31 loc) Β· 1.04 KB
/
Copy pathindex.js
File metadata and controls
38 lines (31 loc) Β· 1.04 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
import express from 'express';
import { connectDB } from './db/db.js';
import { seedDefaultData } from './utils/seed.js';
import { studentRouter } from './routes/student.js';
import { serveSwagger } from './lib/swagger.js';
export async function startServer(port) {
const app = express();
app.use(express.json());
try {
console.log("π Starting server...");
const db = await connectDB();
console.log('π Database connected!');
await seedDefaultData(db);
console.log('πΎ Default data seeded!');
// Attach db to every request
app.use((req, res, next) => {
req.db = db;
next();
});
app.use('/students', studentRouter);
serveSwagger(app);
app.listen(port, () => {
console.log(`π SQL API ready at http://localhost:${port}`);
console.log(`π Swagger at http://localhost:${port}/docs`);
});
} catch (error) {
console.error('β Failed to start the server:', error);
process.exit(1); // Exits if there's any error in the server setup
}
}
startServer(8000);