Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// 1. Install bodyParsers for the request types your API will support
app.use(bodyParser.urlencoded());
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 spec = path.join(__dirname, 'openapi.yaml');
app.use('/spec', express.static(spec));
// 2. Install the OpenApiValidator on your express app
new OpenApiValidator({
apiSpec: './example.yaml',
validateResponses: true,
// securityHandlers: {
// ApiKeyAuth: (req, scopes, schema) => true,
// },
}).install(app);
let id = 3;
// 3. Add routes
app.get('/v1/pets', function(req, res, next) {
res.json(pets);
});
app.post('/v1/pets', function(req, res, next) {
res.json({ id: id++, ...req.body });
});