Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. ...
  2.  
  3. const app = new koa();
  4.  
  5. // Modify router name to 'userRouter' and alter the prefix
  6. const userRouter = new koaRouter({ prefix: '/api/users' });
  7.  
  8. mongoose.connect('mongodb://127.0.0.1:27017/koa2');
  9.  
  10. // Adjust the router name and route accordingly
  11. userRouter.get('/', async (ctx) => {
  12. let user = await User.find();
  13. ctx.body = user;
  14. });
  15.  
  16. // Same here
  17. userRouter.get('/:id', async (ctx) => {
  18. let user = await User.findOne(ctx.params.id);
  19. ctx.body = user;
  20. });
  21.  
  22. // Don't forget to modify the route name here as well
  23. app.use(userRouter.routes());
  24.  
  25. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement