使用Koa作为Nuxt服务器中间件

时间:2019-10-08 07:56:19

标签: nuxt.js koa

我正在尝试将Koa用作nuxt的服务器中间件。当我向localhost:3000 / api / users发出请求时,没有响应,找不到路由。我的代码有什么问题?

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

1 个答案:

答案 0 :(得分:2)

使用app.callback()而不是 var text = "in the park"; var m = text.match(/\b(?:in|at)\s+(\w.*)/); if (m) { console.log(m[1]); }作为处理程序:

app