-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
68 lines (52 loc) · 1.64 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
import dotenv from 'dotenv'
dotenv.config()
import express from 'express'
import { connectToDB } from './database/db.js'
import { handleErrors } from './middlewares/errorHandler.js'
import { authRoutes } from './routes/auth-routes.js'
import helmet from 'helmet'
import cors from 'cors'
import morgan from 'morgan'
import rateLimit from 'express-rate-limit'
import swaggerUi from 'swagger-ui-express';
import swaggerFile from './swagger/swagger-output.json' assert { type: 'json' };
const app = express()
const port = process.env.PORT
// helmet to secure app by setting http response headers
app.use(helmet());
app.use(morgan('tiny'))
let limiter = rateLimit({
max: 1000,
windowMs: 60 * 60 * 1000,
message: "We have received too many requests from this IP. Please try again after one hour."
})
// middlewares
app.use('/api', limiter)
app.use(express.json())
app.use(express.urlencoded({extended: true}))
// cors config
const corsOptions = {
origin: ['http://localhost:5000'],
optionsSuccessStatus: 200,
credentiasl: true,
}
app.use(cors(corsOptions))
// routes
app.use('/api/v1/auth', authRoutes)
// Serve Swagger docs on '/api-docs' route
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerFile));
// home
app.get('/', (req, res) => {
res.json({success: true, message: 'Backend Connected Successfully'})
})
// not found
app.get('*', (req, res) => {
res.json({success: false, message: "Request Not found!"})
})
// error handler
app.use(handleErrors)
// connect to database
connectToDB()
app.listen(port, ()=> {
console.log(`Server running on port ${port}`)
})