-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
57 lines (44 loc) · 1.26 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
const express = require('express')
require('express-async-errors')
// environment variables
require('dotenv').config()
// import custom middlewares
const errorHandler = require('./middleware/error-handler')
const pageNotFound = require('./middleware/not-found-handler')
// Swagger imports
const swaggerUI = require('swagger-ui-express')
const YAML = require('yamljs')
const swaggerDoc = YAML.load('./swagger.yaml')
const app = express();
// express middlewares
app.use(express.json());
app.use(express.urlencoded({ extended: false }))
// route
require('./routes/main')(app)
// default route
app.get('/', (req, res) => {
res.redirect('/api-docs');
})
// configure swagger options, [removing schema]
var options = {
swaggerOptions: {
defaultModelsExpandDepth: -1
}
};
// api docs route to Swagger docs
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerDoc, options));
// error handler
app.use(errorHandler);
app.use(pageNotFound);
const port = process.env.PORT || 3000;
const start = async () => {
try {
app.listen(port, () => {
console.log(`Listening on port: ${port}`)
})
}
catch (error) {
throw new InternalServerError(`An error ocurred while connecting to database.. ${error}`)
}
}
start();