-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (35 loc) · 1.33 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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger/swagger.json');
const createMiddleware = require('swagger-express-middleware');
const opn = require('opn');
import { Mongoose } from './mongoose/mongoose';
import { routes } from './routes/routes';
import { config } from './config/config';
const mongoose = new Mongoose();
mongoose.connect();
const domain = config.application.domain;
const port = config.application.port !== 80 ? `:${config.application.port}` : '';
if(config.application.domain){
swaggerDocument.host = `${domain}${port}`;
}
createMiddleware(swaggerDocument, app, (err, middleware) => {
app.use(middleware.metadata());
app.use(middleware.parseRequest());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use('/api/v1', routes);
// Error handler to display the validation error as HTML
app.use(function(err, req, res, next) {
res.status(err.status);
res.send({message: err.message});
});
app.listen(config.application.port,() => {
opn(`${swaggerDocument.host + config.swagger.path}`);
});
});