This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
60 lines (44 loc) · 1.57 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
'use strict';
var SwaggerTools = require('swagger-tools');
var cors = require('cors');
var YAML = require('yamljs');
var swaggerObject = YAML.load('./api/swagger/swagger.yaml');
// add timestamps in front of log messages
require('console-stamp')(console, {pattern: "m/dd/yy HH:MM:ss.l", label: false});
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(cors());
app.use(bodyParser.json({limit: '50mb'}));
function errorHandler(err, req, res, next) {
if (res.headersSent) {
return next(err);
}
err.err = true;
console.log(err);
res.status(500).json(err);
}
var config = {
appRoot: __dirname, // required config
};
// Initialize the Swagger Middleware
SwaggerTools.initializeMiddleware(swaggerObject, function (middleware) {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata());
// Validate Swagger requests
app.use(middleware.swaggerValidator({
validateResponse: false
}));
// Route validated requests to appropriate controller
app.use(middleware.swaggerRouter({useStubs: true, controllers: './api/controllers'}));
// Serve the Swagger documents and Swagger UI
app.use(middleware.swaggerUi({apiDocs: '/apidocs'}));
app.use(errorHandler);
// Start the server
var port = process.env.PORT || 5000;
var server = app.listen(port, function () {
var host = server.address().address;
var port = server.address().port;
console.log('listening on %s:%s', host, port);
});
});