-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
39 lines (33 loc) · 1.17 KB
/
server.ts
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
39
import express, { json } from 'express';
import { createServer } from 'http';
import cors from 'cors';
import { CONFIG } from './configs/config';
import {
initDbConnection,
initRedisConnection
} from './utils/connection-check';
import { logger } from './configs/winston';
import { errorHandler } from './middlewares/error-handler-middleware';
import { router } from './routes';
import { CONSTANTS } from './utils/constants';
const PORT = CONFIG.SERVER.PORT ?? CONSTANTS.SERVER.DEFAULT_PORT;
const app = express();
app.use(cors());
app.use(json());
app.use('/', router);
// Register the error handling middleware
app.use(errorHandler);
// Wait for both database and Redis connection
Promise.all([initDbConnection(), initRedisConnection()])
.then(() => {
// Create an HTTP server instance from the Express app
const server = createServer(app);
server.listen(PORT, () => {
logger.info(`Server is running on port ${PORT}`);
});
// Handle startup errors
server.on('error', (error) => {
logger.error(`[server] An error occurred while starting the server: ${error}`);
process.exit(1);
});
});