-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.js
37 lines (30 loc) · 891 Bytes
/
server.js
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
// server.js
const { app, connectDB } = require('./app');
const PORT = process.env.PORT || 3000;
const startServer = async () => {
try {
await connectDB();
const server = app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
// Handle server shutdown gracefully
const shutdown = async () => {
console.log('Shutting down server...');
await new Promise((resolve) => {
server.close(resolve);
});
await mongoose.connection.close();
process.exit(0);
};
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
} catch (error) {
console.error('Failed to start server:', error);
process.exit(1);
}
};
// Start server only if not in test environment
if (process.env.NODE_ENV !== 'test') {
startServer();
}
module.exports = { startServer }; // Export for testing purposes