-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
85 lines (63 loc) · 2.14 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
73
74
75
76
77
78
79
80
81
82
83
84
85
require('dotenv').config();
require('express-async-errors');
// extra security packages
const helmet = require('helmet')
const cors = require('cors')
const xss = require('xss-clean')
const rateLimiter = require('express-rate-limit')
// Swagger UI setup
const swaggerUI = require('swagger-ui-express')
const yaml = require('yamljs')
const swaggerDocument = yaml.load('./swagger.yml');
// express
const express = require('express');
const server = express();
// connect DB
const DBConnection = require('./db/connectDB');
// authentication middleware import to protect user
const authenticateUser = require('./middleware/authentication')
// auth & job router imports
const authRouter = require('./routes/authRoute');
const jobsRouter = require('./routes/jobRoute');
// error handler middlewares
const notFoundMiddleware = require('./middleware/not-found');
const errorHandlerMiddleware = require('./middleware/error-handler');
// enable if your app behind a reverse proxy
server.set('trust proxy', 1);
// setup for rate limiter middleware
server.use(rateLimiter({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // limit each IP to 100 requests per windowMs
}))
// to use json data
server.use(express.json());
// middleware setup for additional packages
server.use(helmet())
server.use(cors())
server.use(xss())
server.use(express.static('./public'));
// home route
server.get('/', (req,res) => {
res.send('<h1>Jobs API</h1><a href="/api-docs">Documentation</a>');
})
server.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerDocument))
// login & register routes
server.use('/api/v1/auth',authRouter);
// jobsAPI route
server.use('/api/v1/jobs', authenticateUser, jobsRouter);
server.use(notFoundMiddleware);
server.use(errorHandlerMiddleware);
const portNo = process.env.PORT || 6464;
const Mongo_URI = process.env.mongoURL;
const start = async () => {
try {
await DBConnection(Mongo_URI)
.then(() => console.log('DB Connected'))
server.listen(portNo, () => {
console.log(`Server is listening on port ${portNo}...`);
})
} catch (error) {
console.log(error);
}
}
start();