Created
September 18, 2017 18:40
-
-
Save mfifth/f17c15aed897e18a307979a72cda5947 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 Koa = require('koa'); | |
| const Router = require('koa-router'); | |
| const app = new Koa(); | |
| const router = new Router(); | |
| let users = [ | |
| { | |
| name: "Jacob", | |
| email: "[email protected]", | |
| }, | |
| { | |
| name: "Kenny", | |
| email: "[email protected]", | |
| }, | |
| { | |
| name: "Joe", | |
| email: "[email protected]", | |
| }, | |
| ]; | |
| //localhost:3000 | |
| router.get('/user/:id', ctx => { | |
| ctx.body = users[ctx.params.id]; | |
| }); | |
| router.post('/user/:id', ctx => { | |
| ctx.body = Object.assign(users[ctx.params.id], ctx.request.body); | |
| }); | |
| app.use(require('koa-body')()); | |
| app.use(router.allowedMethods()); | |
| app.use(router.routes()); | |
| app.listen('3000'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment