-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
72 lines (63 loc) · 2.08 KB
/
index.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
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const path = require('path');
const helmet = require('helmet');
const logger = require('./logger');
const otpRoutes = require('./routes/otp');
const connectDB = require('./config/db');
const morgan = require('morgan');
const rateLimit = require('express-rate-limit');
const compression = require('compression');
const errorHandler = require('./middleware/errorHandler');
const app = express();
const PORT = process.env.PORT || 3000;
// Database connection
connectDB();
// Middleware
app.use(helmet());
app.use(cors({ origin: '*' }));
app.use(bodyParser.json());
app.use(express.static('public'));
app.use(express.json());
app.use(morgan('combined'));
app.use(compression());
// Rate Limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
});
app.use(limiter);
app.use(errorHandler);
// Routes
app.use('/api', require('./routes/states'));
app.use('/api', require('./routes/districts'));
app.use('/api', require('./routes/simpleapis'));
app.use('/api', require('./routes/ossinsight'));
app.use('/api', require('./routes/contacts'));
app.use('/api', require('./routes/subscribers'));
app.use('/api', require('./routes/items'));
app.use('/api', require('./routes/books'));
app.use('/api', require('./routes/notes'));
app.use('/api', otpRoutes);
app.use('/api/locations', require('./routes/locations'));
app.use('/api/jokes', require('./routes/jokes'));
app.use('/api/uses', require('./routes/uses'));
// Swagger setup
require('./swagger')(app);
// Error handling
app.use((err, req, res) => {
logger.error(err.stack);
res.status(500).send('Something went wrong!');
});
// Serve static files
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, 'public')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
}
app.listen(PORT, () => {
logger.info(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`);
});