-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
75 lines (66 loc) · 2.07 KB
/
Copy pathserver.js
File metadata and controls
75 lines (66 loc) · 2.07 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
66
67
68
69
70
71
72
73
74
75
const express = require('express');
const cors = require('cors');
const path = require('path');
const cookieParser = require('cookie-parser');
const config = require('./config');
const {
auth,
authRouter,
reviewsRouter,
appsRouter,
analyticsRouter,
syncRouter,
} = require('./routes');
const app = express();
app.use((req, res, next) => {
const p = (req.path || '').toLowerCase();
if (p.includes('.db') || p.includes('/data/') || p.startsWith('/data')) {
return res.status(404).end();
}
next();
});
const allowedOrigins = config.corsOrigins;
app.use(cors({
credentials: true,
origin: allowedOrigins.length ? allowedOrigins : true,
}));
app.use(express.json({ limit: '1mb' }));
app.use(express.urlencoded({ extended: true, limit: '1mb' }));
app.use(cookieParser());
app.use('/api/auth', authRouter);
app.get('/login', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'login.html'));
});
app.use('/api', auth.authenticate);
app.use('/api/reviews', reviewsRouter);
app.use('/api/apps', appsRouter);
app.use('/api', analyticsRouter);
app.use('/api', syncRouter);
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.use((err, req, res, _next) => {
console.error('Unhandled error:', err);
res.status(500).json({ error: config.isProd ? 'Internal server error' : err.message });
});
const server = app.listen(config.port, () => {
console.log(`Server running on http://localhost:${config.port}`);
});
function shutdown(signal) {
console.log(`\n${signal} received. Shutting down gracefully...`);
server.close(() => {
try { require('./lib/db').close(); } catch (e) {}
process.exit(0);
});
setTimeout(() => process.exit(1), 5000);
}
process.on('SIGTERM', () => shutdown('SIGTERM'));
process.on('SIGINT', () => shutdown('SIGINT'));
process.on('uncaughtException', (err) => {
console.error('Uncaught exception:', err);
shutdown('uncaughtException');
});
process.on('unhandledRejection', (reason) => {
console.error('Unhandled rejection:', reason);
});