I am trying to use Koa as server middleware for nuxt. When I make a request to localhost:3000/api/users I get no response, route not found. What is wrong with my code?
nuxt.config.js
serverMiddleware: [
// API middleware
'~/api/index.js'
]
index.js
const Koa = require('koa')
const Api = require('koa-router')
const users = require('./routes/users')
const koa = new Koa()
const api = new Api()
api.use('/users', users.routes())
koa.use(api.routes())
koa.use(api.allowedMethods())
// Export the server middleware
module.exports = {
path: '/api',
handler: koa
}
users.js
const Router = require('koa-router')
const router = new Router()
router.get('/users', ctx => {
ctx.body = 'Hello World'
})
module.exports = router
Use app.callback()
instead of just app
as handler:
import Koa from 'koa'
const app = new Koa()
app.use(ctx => {
ctx.body = 'Hello World'
})
app.listen()
export default {
path: '/example',
handler: app.callback()
}