Created
November 3, 2016 20:08
-
-
Save ujeenator/e40c187c401f9f07d80b9548b59f1ac6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const app = require("koa")(); | |
| const router = require("koa-router")(); | |
| const r = require("rethinkdbdash")(); | |
| const ajv = require("ajv")({ | |
| removeAdditional: true | |
| }); | |
| app.use(require("koa-body")()); | |
| app.use(router.routes()); | |
| const person = { | |
| type: "object", | |
| properties: { | |
| name: { type: "string", minLength: 2, maxLength: 50 }, | |
| email: { type: "string", format: "email" }, | |
| age: { type: "integer" } | |
| }, | |
| required: ["name", "email"], | |
| additionalProperties: false | |
| }; | |
| router.post("/api/people/add", function*() { | |
| let valid = ajv.validate(person, this.request.body); | |
| if (!valid) | |
| this.throw(400, JSON.stringify(ajv.errors)); | |
| this.body = yield r.table("person") | |
| .insert(this.request.body); | |
| }); | |
| app.listen(8000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment