-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
73 lines (42 loc) · 1.76 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
68
69
70
71
72
73
'use strict';
const app = require( 'express' )() ;
const swaggerTools = require( 'swagger-tools' ) ;
const YAML = require( 'yamljs' ) ;
const cors = require( 'cors' ) ;
const morgan = require( 'morgan' ) ;
const appRoot = require( 'app-root-path' ) ;
process.env[ 'NODE_CONFIG_DIR' ] = appRoot + '/environment/' ;
const swaggerConfig = YAML.load('./api/swagger/swagger.yaml' ) ;
const databaseUnityOperations = require( './database/operation/databaseUnityOperations' ) ;
app.use( cors() ) ;
swaggerTools.initializeMiddleware( swaggerConfig, function ( middleware ) {
app.use( middleware.swaggerMetadata() ) ;
app.use( middleware.swaggerValidator( { options: [Object, Array, String, Number, Boolean] } ) ) ;
app.use( function( error, request, response, next ) {
let message = '' ;
if ( error.code === 'SCHEMA_VALIDATION_FAILED' ) {
if( error && error.results && error.results.errors ) {
error.results.errors.forEach( function ( error ) {
message = 'Validation Error is : ' + error.message + ' ' + error.path.toString() + '\n' ;
} ) ;
}
error.stack = 'Result: ' + message + '\n' + error.stack ;
}
next( error ) ;
if( error.stack ) {
response.send( { validationError: message } ) ;
}
} ) ;
const routerConfig = {
controllers: './api/controllers',
useStubs: false
} ;
app.use( middleware.swaggerRouter( routerConfig ) ) ;
app.use( middleware.swaggerUi() ) ;
app.use( morgan( 'combined' ) ) ;
app.listen( 7100, function() {
console.log( 'API Base server started on port 7100' ) ;
databaseUnityOperations.connect() ;
} ) ;
} ) ;
module.exports = app ;