BinCode/server.js

42 lines
880 B
JavaScript
Raw Normal View History

2025-01-17 20:05:21 +00:00
require('dotenv').config();
const express = require('express');
const rateLimit = require('express-rate-limit');
const { initializeDb } = require('./db/connect');
const routes = require('./routes');
const app = express();
2025-01-17 20:14:09 +00:00
app.set('trust proxy', true);
2025-01-17 20:05:21 +00:00
app.use(rateLimit({
2025-01-17 20:14:09 +00:00
windowMs: 5 * 60 * 1000, // 5min
max: 100, // 100 request per IP
2025-01-17 20:05:21 +00:00
}));
app.use(express.json());
app.use(express.static('public'));
app.use('/api', routes);
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: 'Something broke!' });
});
const PORT = process.env.PORT || 3000;
const startServer = async () => {
try {
await initializeDb();
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
} catch (error) {
console.error('Failed to start server:', error);
process.exit(1);
}
};
2025-01-17 20:14:09 +00:00
startServer();