Skip to content

Commit

Permalink
enhance example
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Dec 1, 2019
1 parent e7e217d commit 3ef9a47
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion example/example.yaml → example/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 10 additions & 8 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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;

0 comments on commit 3ef9a47

Please sign in to comment.