Skip to content

Commit

Permalink
async pass 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Nov 29, 2019
1 parent 9d174ef commit 754fbf9
Show file tree
Hide file tree
Showing 14 changed files with 1,221 additions and 176 deletions.
98 changes: 50 additions & 48 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,59 +29,61 @@ new OpenApiValidator({
// securityHandlers: {
// ApiKeyAuth: (req, scopes, schema) => true,
// },
}).install(app);
})
.install(app)
.then(() => {
let id = 3;
// 3. Add routes
app.get('/v1/pets', function(req, res, next) {
res.json(pets);
});

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 });
});

app.post('/v1/pets', function(req, res, next) {
res.json({ id: id++, ...req.body });
});
app.get('/v1/pets/:id', function(req, res, next) {
const id = req.params.id;
const r = pets.filter(p => p.id === id);
if (id === 99) {
// return a response that does not match the spec
return res.json({ bad_format: 'bad format' });
}
return r.length > 0
? res.json(r)
: res.status(404).json({ message: 'not found', errors: [] });
});

app.get('/v1/pets/:id', function(req, res, next) {
const id = req.params.id;
const r = pets.filter(p => p.id === id);
if (id === 99) {
// return a response that does not match the spec
return res.json({ bad_format: 'bad format' });
}
return r.length > 0
? res.json(r)
: res.status(404).json({ message: 'not found', errors: [] });
});
// 3a. Add a route upload file(s)
app.post('/v1/pets/:id/photos', function(req, res, next) {
// DO something with the file
// files are found in req.files
// non file multipar params are in req.body['my-param']
console.log(req.files);

// 3a. Add a route upload file(s)
app.post('/v1/pets/:id/photos', function(req, res, next) {
// DO something with the file
// files are found in req.files
// non file multipar params are in req.body['my-param']
console.log(req.files);
res.json({
files_metadata: req.files.map(f => ({
originalname: f.originalname,
encoding: f.encoding,
mimetype: f.mimetype,
// Buffer of file conents
// buffer: f.buffer,
})),
});
});

res.json({
files_metadata: req.files.map(f => ({
originalname: f.originalname,
encoding: f.encoding,
mimetype: f.mimetype,
// Buffer of file conents
// buffer: f.buffer,
})),
});
});
// 4. Create a custom error handler
app.use((err, req, res, next) => {
// format errors
console.error(err);
res.status(err.status || 500).json({
message: err.message,
errors: err.errors,
});
});

// 4. Create a custom error handler
app.use((err, req, res, next) => {
// format errors
res.status(err.status || 500).json({
message: err.message,
errors: err.errors,
const server = http.createServer(app);
server.listen(3000);
console.log('Listening on port 3000');
});
});

const server = http.createServer(app);
server.listen(3000);
console.log('Listening on port 3000');

module.exports = app;
Loading

0 comments on commit 754fbf9

Please sign in to comment.