-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
44 lines (35 loc) · 1.19 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
'use strict';
const config = require('config');
var SwaggerExpress = require('swagger-express-mw');
var app = require('express')();
module.exports = app; // for testing
SwaggerExpress.create(
{appRoot: __dirname},
function(err, swaggerExpress){
if (err) { throw err; }
// enable SwaggerUI
app.use(swaggerExpress.runner.swaggerTools.swaggerUi());
// install middleware
swaggerExpress.register(app);
// Custom error handler that returns JSON
app.use(function(err, req, res, next) {
console.log({err});
if (typeof err !== 'object') {
// If the object is not an Error, create a representation that appears to be
err = {
message: String(err) // Coerce to string
};
} else {
// Ensure that err.message is enumerable (It is not by default)
Object.defineProperty(err, 'message', { enumerable: true });
}
// Return a JSON representation of #/definitions/ErrorResponse
res.setHeader('Content-Type', 'application/json');
res.status(500).json({message: JSON.stringify(err)});
});
var port = config.port;
app.listen(port, () => {
console.log('Listening on:', port);
});
}
);