-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
67 lines (48 loc) · 1.66 KB
/
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
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
const express = require('express');
const https = require('https');
const fs = require('fs');
const path = require('path');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const cors = require('cors');
const morgan = require('morgan');
const compression = require('compression');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
const config = require('./config');
const logger = require('./services/logger');
const connectDB = require('./services/database');
const errorHandler = require('./middleware/errorHandler');
const app = express();
// Connect to Database
connectDB();
// Middleware
app.use(helmet());
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(morgan('combined', { stream: { write: message => logger.info(message.trim()) } }));
app.use(compression());
// Rate limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100
});
app.use(limiter);
app.use(express.static(path.join(__dirname, 'public')));
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use('/auth', require('./routes/auth'));
app.use('/user', require('./routes/user'));
app.use('/data', require('./routes/data'));
app.use(errorHandler);
const httpsOptions = {
key: fs.readFileSync(config.sslKeyPath),
cert: fs.readFileSync(config.sslCertPath)
};
https.createServer(httpsOptions, app).listen(config.httpsPort, () => {
logger.info(`HTTPS Server running on port ${config.httpsPort}`);
});
app.listen(config.httpPort, () => {
logger.info(`HTTP Server running on port ${config.httpPort}`);
});
module.exports = app;