-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
56 lines (44 loc) · 1.6 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
const express = require('express')
const app = express()
const config = require('./config/application.config')
const messages = require('./config/messages.config')
const CardsController = require('./routes/cards.route')
const DatabaseController = require('./routes/database.route')
const ImagesController = require('./routes/images.route')
const mongoose = require('mongoose')
const helmet = require('helmet')
const cors = require('cors')
mongoose.connect(config.MONGO.URL, config.MONGO.CONFIG)
.then(() => {
console.log('MongoDB connected with success.');
})
.catch(error => {
console.error('mongoDB connection error:', error);
process.exit(1);
});
// API Security (https://helmetjs.github.io)
app.use(helmet())
// CORS
app.use(
cors({
credentials: true,
origin: config.APP.IS_DEV ? true : config.APP.CLIENT_URL,
})
)
// https://stackoverflow.com/questions/23413401/what-does-trust-proxy-actually-do-in-express-js-and-do-i-need-to-use-it
app.set('trust proxy', true)
// Load the json module to parse POST and PUT request and increase the limit of the body
app.use(express.json({ limit: '50mb' }))
app.use(express.urlencoded({ limit: '50mb', extended: true }))
// Load all the controllers
app.use('/cards', CardsController)
app.use('/database', DatabaseController)
app.use('/images', ImagesController)
const swaggerUi = require('swagger-ui-express')
const swaggerDocument = require('./swagger.json')
app.use('/', swaggerUi.serve, swaggerUi.setup(swaggerDocument))
// Handle 404 request
app.use((request, response) => {
response.status(404).send({ message: messages.not_found })
})
module.exports = app