Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ...
- const app = new koa();
- // Modify router name to 'userRouter' and alter the prefix
- const userRouter = new koaRouter({ prefix: '/api/users' });
- mongoose.connect('mongodb://127.0.0.1:27017/koa2');
- // Adjust the router name and route accordingly
- userRouter.get('/', async (ctx) => {
- let user = await User.find();
- ctx.body = user;
- });
- // Same here
- userRouter.get('/:id', async (ctx) => {
- let user = await User.findOne(ctx.params.id);
- ctx.body = user;
- });
- // Don't forget to modify the route name here as well
- app.use(userRouter.routes());
- ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement