-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (55 loc) · 2.05 KB
/
app.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
const express = require('express')
require('dotenv').config()
require('express-async-errors')
const connect = require('./db/connect')
const morgan = require('morgan')
const errorMiddleware = require('./middleware/errorHandler')
const notFoundMiddleware = require('./middleware/notFoundHandler')
const {InternalServerError} = require('./error/index')
// 3rd party security middlewares
const helmet = require('helmet') // secure express apps by setting various http headers
const cors = require('cors') // allows access to public domain
const xss = require('xss-clean') // sanitize or clean req.body, req.params and req.query and secure cross origin request
const rateLimiter = require('express-rate-limit') // limit calls to functions
// Swagger
const swaggerUI = require('swagger-ui-express')
const YAML = require('yamljs')
const swaggerDoc = YAML.load('./swagger.yaml')
const app = express();
// express middlewares
app.set('trust proxy', 1) // needed ONLY IF this will be hosted in reverse proxy (heroku, AWS etc.)
app.use(rateLimiter({
windowsMs: 15 * 60 * 1000, // 15 minutes converted to milliseconds. 900,000 ms = 15 minutes
max: 100 // limit each IP 100 requests per 15 minutes(windowsMs)
}));
app.use(helmet());
app.use(cors());
app.use(xss());
app.use(express.json());
app.use(express.urlencoded({extended: false}))
app.use(morgan('tiny'))
// default route
app.get('/', (req, res) => {
res.send('<h1>Jobs API</h1><a href="/api-docs">Documentation</a>');
})
// api docs route // Swagger docs
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerDoc));
// route
require('./route/project-route')(app)
// error handlers (custom middleware)
app.use(notFoundMiddleware);
app.use(errorMiddleware);
// connect
const port = process.env.PORT || 5000;
const start = async () => {
try {
await connect(process.env.MONGO_URI);
app.listen(port, ()=> {
console.log(`Listening on port: ${port}`)
})
}
catch (error) {
throw new InternalServerError('An error ocurred while connecting to database..')
}
}
start();