In the project created by create-nuxt-app, accessing the koa API displays page error 404.

koa API: http://xxx,xxxxxx.com/weapp/t.

Direct address bar access display 404

it should be that nuxt accesses the weapp/token page under the page project by default. If I want to access the koa interface in the address bar, how can I?


koa-trie-router


in the APP created by nuxt-cli, if you want to access the interface API, you have to configure Koa-router

const router = require('../routes')
// [koa-router][1]
app.use(async (ctx, next) => {
    //    URL  'api/' next koa-router
    //  nuxt vue-router
    if (/api\//.test(ctx.request.url)) {
      await next()
    } else {
      ctx.status = 200
      return new Promise((resolve, reject) => {
        ctx.res.on('close', resolve)
        ctx.res.on('finish', resolve)
        nuxt.render(ctx.req, ctx.res, promise => {
          // nuxt.render passes a rejected promise into callback on error.
          promise.then(resolve).catch(reject)
        })
      })
    }
  })

  app.use(router.routes())
Menu