-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
115 lines (97 loc) · 2.89 KB
/
app.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// app.js
import express from 'express';
import dotenv from 'dotenv';
import bodyParser from 'body-parser';
import cors from 'cors';
import helmet from 'helmet';
import cookieParser from 'cookie-parser';
import morgan from 'morgan';
import logger from './config/logger.js';
import redisClient from './config/redisClient.js';
import { connectDB } from './config/db.js';
import { errorHandler } from './middleware/errorHandler.js';
import authRouter from './routes/authRoutes.js';
import userRoutes from './routes/userRoutes.js';
import boardRouter from './routes/boardRoutes.js';
import postsRouter from './routes/postRoutes.js';
import reportRoutes from './routes/reportRoutes.js';
dotenv.config();
const app = express();
// Security middlewares
app.use(helmet());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// CORS configuration
app.use(
cors({
origin: process.env.CLIENT_URL || 'http://localhost:5500',
credentials: true,
})
);
// Body parser and cookie parser
app.use(express.json());
app.use(cookieParser());
// HTTP request logging with Morgan integrated with Winston
const stream = {
write: (message) => logger.http(message.trim()),
};
app.use(morgan('combined', { stream }));
// Routes
app.use('/api/auth', authRouter);
app.use('/api/user', userRoutes);
app.use('/api/boards', boardRouter);
app.use('/api/posts', postsRouter);
app.use('/api/reports', reportRoutes);
// Health check route
app.get('/', (req, res) => {
res.send('Hello, We!');
});
// Error handling middleware
app.use(errorHandler);
// Handle uncaught exceptions
process.on('uncaughtException', (error) => {
logger.error('Uncaught Exception: %o', error);
process.exit(1);
});
// Handle unhandled promise rejections
process.on('unhandledRejection', (reason, promise) => {
logger.error('Unhandled Rejection at: %o, reason: %o', promise, reason);
process.exit(1);
});
// Function to wait for Redis client to be ready
const waitForRedisReady = () => {
return new Promise((resolve, reject) => {
if (redisClient.status === 'ready') {
logger.info('Redis client is already ready');
resolve();
} else {
redisClient.once('ready', () => {
logger.info('Connected to Redis');
resolve();
});
redisClient.once('error', (err) => {
logger.error('Redis error: %o', err);
reject(err);
});
}
});
};
// Start the server after Redis and MongoDB are ready
const startServer = async () => {
try {
// Wait for both MongoDB and Redis to be ready
await Promise.all([
connectDB().then(() => logger.info('Connected to MongoDB')),
waitForRedisReady(),
]);
const PORT = process.env.PORT || 5500;
app.listen(PORT, () => {
logger.info(`Server running on PORT: ${PORT}`);
});
} catch (error) {
logger.error('Error starting server: %o', error);
process.exit(1);
}
};
startServer();
export default app;