-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (54 loc) · 1.89 KB
/
Copy pathserver.js
File metadata and controls
65 lines (54 loc) · 1.89 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
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
// Catch synchronous "bugs" immediately
process.on('uncaughtException', (err) => {
console.error('UNCAUGHT EXCEPTION! 💥', err.name, err.message);
process.exit(1);
});
require('dotenv').config();
const express = require('express');
const helmet = require('helmet');
const { corsConfig } = require('./config/corsConfig');
const AppError = require('./utils/appError');
const globalErrorHandler = require('./controllers/errorController');
const { initRateCron } = require('./services/rateService');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(corsConfig());
app.use(helmet());
// --- HEALTH CHECK ENDPOINT ---
app.get('/health', (req, res) => {
const healthcheck = {
status: 'UP',
uptime: process.uptime(), // Returns the number of seconds the server has been running
timestamp: new Date().toISOString(),
memoryUsage: process.memoryUsage() // For monitoring memory leaks
};
try {
res.status(200).json(healthcheck);
} catch (error) {
healthcheck.status = 'DOWN';
res.status(503).json(healthcheck);
}
});
app.use('/auth', require('./routes/authRouter'));
app.use('/users', require('./routes/userRouter'));
app.use('/products', require('./routes/productRouter'));
app.use('/rates', require('./routes/rateRouter'));
app.use('/cart', require('./routes/cartRouter'));
app.use('/', require('./routes/orderRouter'));
app.all(/.*/, (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} on this server!`, 404));
});
app.use(globalErrorHandler);
const PORT = process.env.PORT || 3000;
const server = app.listen(PORT, () => {
console.log(`🚀 Server started on port ${PORT}`);
initRateCron();
});
// Catch asynchronous "unhandled promises"
process.on('unhandledRejection', (err) => {
console.error('UNHANDLED REJECTION! 💥', err.name, err.message);
server.close(() => {
process.exit(1);
});
});