games-express-api/app.js

58 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-09-12 15:48:27 -04:00
import 'dotenv/config'
import 'colors'
import express from 'express'
import cors from 'cors'
import helmet from 'helmet'
import cookieParser from 'cookie-parser'
import mongoSanitize from 'express-mongo-sanitize'
import xss from 'xss-clean'
import rateLimit from 'express-rate-limit'
import hpp from 'hpp'
import morgan from 'morgan'
import errorHandler from './middleware/error.js'
import games from './routes/games.js'
import adminGames from './routes/adminGames.js'
import tags from './routes/tags.js'
import auth from './routes/auth.js'
import users from './routes/users.js'
import createAdmin from './scripts/adminUser.js'
import connectDB from './config/db.js'
connectDB().then(x => x)
const app = express()
const whitelist = ['http://localhost:3000', 'http://localhost:5173','https://games.linuxhg.com', 'http://localhost:8000']
const corsOptions = {
origin: (origin, callback) => {
if (whitelist.indexOf(origin) !== -1 || !origin) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
},
}
const limiter = rateLimit({
windowMs: 10 * 60 * 1000, // 10 minutes
max: 100
})
2024-09-12 19:33:14 -04:00
app.use(express.json(), cookieParser(), morgan('dev'), mongoSanitize(), helmet(), xss(), limiter, hpp(), cors())
2024-09-12 15:48:27 -04:00
app.use('/api/admin/games', adminGames)
app.use('/api/games', games)
app.use('/api/tags', tags)
app.use('/api/auth', auth)
app.use('/api/admin/users', users)
app.use(errorHandler)
createAdmin()
export default app