Merge branch 'nodejs-server-improvements' of https://github.com/Granjow/swagger-codegen into Granjow-nodejs-server-improvements

This commit is contained in:
wing328 2017-03-30 22:31:17 +08:00
commit 0baa6cf0e8

View File

@ -1,25 +1,28 @@
'use strict'; 'use strict';
var fs = require('fs'),
path = require('path'),
http = require('http');
var app = require('connect')(); var app = require('connect')();
var http = require('http');
var swaggerTools = require('swagger-tools'); var swaggerTools = require('swagger-tools');
var jsyaml = require('js-yaml'); var jsyaml = require('js-yaml');
var fs = require('fs');
var serverPort = {{serverPort}}; var serverPort = {{serverPort}};
// swaggerRouter configuration // swaggerRouter configuration
var options = { var options = {
swaggerUi: '/swagger.json', swaggerUi: path.join(__dirname, '/swagger.json'),
controllers: './controllers', controllers: path.join(__dirname, './controllers'),
useStubs: process.env.NODE_ENV === 'development' ? true : false // Conditionally turn on stubs (mock mode) useStubs: process.env.NODE_ENV === 'development' // Conditionally turn on stubs (mock mode)
}; };
// The Swagger document (require it, build it programmatically, fetch it from a URL, ...) // The Swagger document (require it, build it programmatically, fetch it from a URL, ...)
var spec = fs.readFileSync('./api/swagger.yaml', 'utf8'); var spec = fs.readFileSync(path.join(__dirname,'api/swagger.yaml'), 'utf8');
var swaggerDoc = jsyaml.safeLoad(spec); var swaggerDoc = jsyaml.safeLoad(spec);
// Initialize the Swagger middleware // Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) { swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain // Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata()); app.use(middleware.swaggerMetadata());
@ -37,4 +40,5 @@ swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort); console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
console.log('Swagger-ui is available on http://localhost:%d/docs', serverPort); console.log('Swagger-ui is available on http://localhost:%d/docs', serverPort);
}); });
}); });