Static GitHub Issues

[2615] How to config server to serve /api ?

prev: [Question] Best way to register a function on window object
next: Access window object from Vuex store file

https://nuxtjs.org/api/configuration-servermiddleware shows if I define handler function in serverMiddleware spec, I can do that. But the question is how? Here's my setup:

server.js:

const 
fastify = require('fastify')
,{Nuxt, Builder} = require('nuxt')
,config = require('./nuxt.config.js')
,host = process.env.HOST || '127.0.0.1'
,port = process.env.PORT || 3000
,nuxt = new Nuxt(config)
,app = fastify()

// Enable live build & reloading on dev
if (config.dev) {
  new Builder(nuxt).build()
}

// __ONLY one of these work as they conflict__:
// app.register(require('./api/index'), {prefix: '/api'})
// app.use(nuxt.render)

app.listen(port, host)

api/index.js:

module.exports = async function (fastify, opts, next) {
  fastify.get('/', (req, reply) => {
    reply.send({ hello: 'world' })
  })
}

thanks.

<!--cmty--><!--cmty_prevent_hook--><div align="right"><sub><em>This question is available on <a href="https://nuxtjs.cmty.io">Nuxt.js</a> community (<a href="https://nuxtjs.cmty.io/nuxt/nuxt.js/issues/c2275">#c2275</a>)</em></sub></div>