We established the integration and structured the server-side directory in the previous section. Now let's refine some API routes and other middleware on our API in the following steps:
- Install the Koa Router and Koa Static packages via npm:
$ npm i koa-route
$ npm i koa-static
- Create a server-side config file:
// server/config/index.js
export default {
static_dir: {
root: '../static'
}
}
- Create a routes.js file in the /server/ directory for defining routes that we will expose to the public with some dummy user data:
// server/routes.js
import Router from 'koa-router'
const router = new Router({ prefix: '/api' })
const users = [
{ id: 1, name: 'Alexandre' },
{ id: 2, name: 'Pooya' },
{ id: 3, name: 'Sébastien' }
]
router.get('/', async (ctx, next) => {
ctx.type = 'json'
ctx.body = {
message: 'Hello World!'
}
})
router.get(&apos...