From 3ef9a477c8608c17c94c8d4935f67e8a469f9176 Mon Sep 17 00:00:00 2001 From: Carmine DiMascio Date: Sat, 30 Nov 2019 23:11:47 -0500 Subject: [PATCH] enhance example --- example/{example.yaml => api.yaml} | 2 +- example/app.js | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) rename example/{example.yaml => api.yaml} (99%) diff --git a/example/example.yaml b/example/api.yaml similarity index 99% rename from example/example.yaml rename to example/api.yaml index f010b6fa..84b53a27 100644 --- a/example/example.yaml +++ b/example/api.yaml @@ -8,7 +8,7 @@ info: name: Apache 2.0 url: https://www.apache.org/licenses/LICENSE-2.0.html servers: - - url: http://petstore.swagger.io/v1 + - url: /v1 paths: /pets: get: diff --git a/example/app.js b/example/app.js index 15916546..283ce54a 100644 --- a/example/app.js +++ b/example/app.js @@ -6,7 +6,10 @@ const logger = require('morgan'); const http = require('http'); const { pets } = require('./pets'); const { OpenApiValidator } = require('express-openapi-validator'); + +const port = 3000; const app = express(); +const apiSpec = path.join(__dirname, 'api.yaml'); // 1. Install bodyParsers for the request types your API will support app.use(bodyParser.urlencoded({ extended: false })); @@ -17,15 +20,14 @@ app.use(logger('dev')); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); -const spec = path.join(__dirname, 'example.yaml'); -app.use('/spec', express.static(spec)); +app.use('/spec', express.static(apiSpec)); // 2. Install the OpenApiValidator on your express app new OpenApiValidator({ - apiSpec: './example.yaml', + apiSpec, validateResponses: true, // securityHandlers: { - // ApiKeyAuth: (req, scopes, schema) => true, + // ApiKeyAuth: (req, scopes, schema) => { ... }, // }, }) .install(app) @@ -43,7 +45,7 @@ new OpenApiValidator({ const pet = pets.findById(req.params.id); return pet ? res.json(pet) - : res.status(404).json({ message: 'not found', errors: [] }); + : res.status(404).json({ message: 'not found' }); }); // 3a. Add a route upload file(s) @@ -73,8 +75,8 @@ new OpenApiValidator({ }); }); - const server = http.createServer(app); - server.listen(3000); - console.log('Listening on port 3000'); + http.createServer(app).listen(port); + console.log(`Listening on port ${port}`); }); + module.exports = app;