Skip to content

Instantly share code, notes, and snippets.

@mfifth
Created September 18, 2017 18:40
Show Gist options
  • Select an option

  • Save mfifth/f17c15aed897e18a307979a72cda5947 to your computer and use it in GitHub Desktop.

Select an option

Save mfifth/f17c15aed897e18a307979a72cda5947 to your computer and use it in GitHub Desktop.
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