forked from loafle/openapi-generator-original
update nodejs express server samples
This commit is contained in:
parent
86a293e0b6
commit
b80461373a
@ -43,7 +43,7 @@ class Controller {
|
||||
static collectFile(request, fieldName) {
|
||||
let uploadedFileName = '';
|
||||
if (request.files && request.files.length > 0) {
|
||||
const fileObject = request.files.find(file => file.fieldname === fieldName);
|
||||
const fileObject = request.files.find((file) => file.fieldname === fieldName);
|
||||
if (fileObject) {
|
||||
const fileArray = fileObject.originalname.split('.');
|
||||
const extension = fileArray.pop();
|
||||
@ -70,7 +70,7 @@ class Controller {
|
||||
|
||||
static collectRequestParams(request) {
|
||||
const requestParams = {};
|
||||
if (request.openapi.schema.requestBody !== undefined) {
|
||||
if (request.openapi.schema.requestBody !== null) {
|
||||
const { content } = request.openapi.schema.requestBody;
|
||||
if (content['application/json'] !== undefined) {
|
||||
const requestBodyName = camelCase(this.getRequestBodyName(request));
|
||||
@ -89,15 +89,17 @@ class Controller {
|
||||
}
|
||||
}
|
||||
|
||||
request.openapi.schema.parameters.forEach((param) => {
|
||||
if (param.in === 'path') {
|
||||
requestParams[param.name] = request.openapi.pathParams[param.name];
|
||||
} else if (param.in === 'query') {
|
||||
requestParams[param.name] = request.query[param.name];
|
||||
} else if (param.in === 'header') {
|
||||
requestParams[param.name] = request.headers[param.name];
|
||||
}
|
||||
});
|
||||
if (request.openapi.schema.parameters !== undefined) {
|
||||
request.openapi.schema.parameters.forEach((param) => {
|
||||
if (param.in === 'path') {
|
||||
requestParams[param.name] = request.openapi.pathParams[param.name];
|
||||
} else if (param.in === 'query') {
|
||||
requestParams[param.name] = request.query[param.name];
|
||||
} else if (param.in === 'header') {
|
||||
requestParams[param.name] = request.headers[param.name];
|
||||
}
|
||||
});
|
||||
}
|
||||
return requestParams;
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@ const express = require('express');
|
||||
const cors = require('cors');
|
||||
const cookieParser = require('cookie-parser');
|
||||
const bodyParser = require('body-parser');
|
||||
const { OpenApiValidator } = require('express-openapi-validator');
|
||||
const OpenApiValidator = require('express-openapi-validator');
|
||||
const logger = require('./logger');
|
||||
const config = require('./config');
|
||||
|
||||
@ -32,11 +32,11 @@ class ExpressServer {
|
||||
this.app.use(express.json());
|
||||
this.app.use(express.urlencoded({ extended: false }));
|
||||
this.app.use(cookieParser());
|
||||
//Simple test to see that the server is up and responding
|
||||
// Simple test to see that the server is up and responding
|
||||
this.app.get('/hello', (req, res) => res.send(`Hello World. path: ${this.openApiPath}`));
|
||||
//Send the openapi document *AS GENERATED BY THE GENERATOR*
|
||||
// Send the openapi document *AS GENERATED BY THE GENERATOR*
|
||||
this.app.get('/openapi', (req, res) => res.sendFile((path.join(__dirname, 'api', 'openapi.yaml'))));
|
||||
//View the openapi document in a visual interface. Should be able to test from this page
|
||||
// View the openapi document in a visual interface. Should be able to test from this page
|
||||
this.app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(this.schema));
|
||||
this.app.get('/login-redirect', (req, res) => {
|
||||
res.status(200);
|
||||
@ -46,30 +46,28 @@ class ExpressServer {
|
||||
res.status(200);
|
||||
res.json(req.query);
|
||||
});
|
||||
this.app.use(
|
||||
OpenApiValidator.middleware({
|
||||
apiSpec: this.openApiPath,
|
||||
operationHandlers: path.join(__dirname),
|
||||
fileUploader: { dest: config.FILE_UPLOAD_PATH },
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
launch() {
|
||||
new OpenApiValidator({
|
||||
apiSpec: this.openApiPath,
|
||||
operationHandlers: path.join(__dirname),
|
||||
fileUploader: { dest: config.FILE_UPLOAD_PATH },
|
||||
}).install(this.app)
|
||||
.catch(e => console.log(e))
|
||||
.then(() => {
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
this.app.use((err, req, res, next) => {
|
||||
// format errors
|
||||
res.status(err.status || 500).json({
|
||||
message: err.message || err,
|
||||
errors: err.errors || '',
|
||||
});
|
||||
});
|
||||
|
||||
http.createServer(this.app).listen(this.port);
|
||||
console.log(`Listening on port ${this.port}`);
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
this.app.use((err, req, res, next) => {
|
||||
// format errors
|
||||
res.status(err.status || 500).json({
|
||||
message: err.message || err,
|
||||
errors: err.errors || '',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
http.createServer(this.app).listen(this.port);
|
||||
console.log(`Listening on port ${this.port}`);
|
||||
}
|
||||
|
||||
async close() {
|
||||
if (this.server !== undefined) {
|
||||
|
@ -19,7 +19,7 @@
|
||||
"cookie-parser": "^1.4.4",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.16.4",
|
||||
"express-openapi-validator": "^3.9.1",
|
||||
"express-openapi-validator": "^4.13.8",
|
||||
"js-yaml": "^3.3.0",
|
||||
"ono": "^5.0.1",
|
||||
"openapi-sampler": "^1.0.0-beta.15",
|
||||
|
Loading…
x
Reference in New Issue
Block a user