Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimascio committed Dec 18, 2019
1 parent 0056db7 commit 77e36a0
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions README.md
Expand Up @@ -680,45 +680,47 @@ app.use((err, req, res, next) => {
It may be useful to serve multiple APIs with separate specs via single service. An exampe might be an API that serves both `v1` and `v2` from the samee service. The sample code below show how one might accomplish this.
```javascript
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const http = require('http');
const { OpenApiValidator } = require('express-openapi-validator');

async function main() {
// 1. initialize express and any body parsers
app = express();
app.use(bodyParser.urlencoded());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.text());
app.use(bodyParser.json());

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

const versions = [1, 2];

for (let v of versions) {
let apiSpec = path.join(__dirname, `v${v}.yaml`);

// 2. install the validator for each api version
await new OpenApiValidator({
apiSpec,
}).install(app);

// 3. init routes for v1 and v2
if (v === 1) routesV1(app);
if (v === 2) routesV2(app);

v += 1;
routes(app, v++);
}

http.createServer(app).listen(3000);
http.createServer(app).listen(3000);
console.log('Listening on port 3000');
}

async function routes(app, v) {
if (v === 1) routesV1(app);
if (v === 2) routesV2(app);
}

async function routesV1(app, v) {
async function routesV1(app) {
const v = '/api/v1';
app.post(`${v}/annotations`, (req, res, next) => {
res.json({ ...req.body, version: v });
res.json({ ...req.body, annotations: v, method: 'post' });
});

app.use((err, req, res, next) => {
// format error
res.status(err.status || 500).json({
message: err.message,
errors: err.errors,
Expand All @@ -729,9 +731,11 @@ async function routesV1(app, v) {
async function routesV2(app) {
const v = '/api/v2';
app.post(`${v}/annotations`, (req, res, next) => {
res.json({ ...req.body, version: v });
res.json({ ...req.body, annotations: v, method: 'post' });
});

app.use((err, req, res, next) => {
// format error
res.status(err.status || 500).json({
message: err.message,
errors: err.errors,
Expand Down

0 comments on commit 77e36a0

Please sign in to comment.