Skip to content

Instantly share code, notes, and snippets.

@slaveofcode
Last active July 24, 2018 11:12
Show Gist options
  • Save slaveofcode/eb88b5239979b981013e1ca7f72b2d28 to your computer and use it in GitHub Desktop.
Save slaveofcode/eb88b5239979b981013e1ca7f72b2d28 to your computer and use it in GitHub Desktop.
Example RESTful API on Express with Joi
async function handler(req, res, next) {
const userSchema = Joi.object().keys({
name: Joi.string().required(),
age: Joi.number().integer()
});
const params = req.params;
const {error, validValues} = Joi.validate(params, userSchema);
let result = null;
if (!error)
result = await db.User.create(validValues);
return res.json(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment