title: How to create api routes with Fastify and Nuxt?
body: <p>Fastify changed a lot since the last time I played with it. Also, I read that we should use Nuxt <strong>serverMiddleware</strong> to add api routes but I couldn't find any example on how to proceed.</p>
  
  <p>Below there is my index file to start Fastify. I tried adding a route in it before nuxt with this line:</p>
  
  <pre><code>fastify.register(require('./routesIndex'), { prefix: '/api' })
  </code></pre>
  
  <p><strong>routesIndex.js</strong> as per the new fastify documentation:</p>
  
  <pre><code>async function routes(fastify, options) {
      fastify.register(require('./fetchRemote'))
      fastify.register(require('./domains'))
  }
  
  module.exports = routes
  </code></pre>
  
  <p>To be thorough, the <strong>domains.js</strong> file:</p>
  
  <pre><code>async function routes(fastify, options) {
      const database = fastify.mongo.db('db')
      const collection = database.collection('test')
  
      fastify.get('/domains/list', async (request, reply) =&gt; {
          return { hello: 'world' }
      })
  
      fastify.get('/domains/:id', async (request, reply) =&gt; {
          const result = await collection.findOne({ id: request.params.id })
          if (result.value === null) {
              throw new Error('Invalid value')
          }
          return result.value
      })
  }
  
  module.exports = routes
  </code></pre>
  
  <p><strong>server/index.js</strong>:</p>
  
  <pre><code>const { Nuxt, Builder } = require('nuxt')
  const fastify = require('fastify')({
    logger: true
  })
  
  fastify.register(require('./db'), {
    url: 'mongodb://localhost:27017'
  })
  
  // Import and Set Nuxt.js options
  const config = require('../nuxt.config.js')
  config.dev = !(process.env.NODE_ENV === 'production')
  
  async function start() {
    // Instantiate nuxt.js
    const nuxt = new Nuxt(config)
  
    const {
      host = process.env.HOST || '127.0.0.1',
      port = process.env.PORT || 3000
    } = nuxt.options.server
  
    // Build only in dev mode
    if (config.dev) {
      const builder = new Builder(nuxt)
      await builder.build()
    } else {
      await nuxt.ready()
    }
  
    fastify.use(nuxt.render)
  
    fastify.listen(port, host, (err, address) =&gt; {
      if (err) {
        fastify.log.error(err)
        process.exit(1)
      }
    })
  }
  
  start()
  </code></pre>
  
  <p>The <strong>/api</strong> route is not found in the browser. But then I read I should use serverMiddleware inside <strong>nuxt.config.js</strong>:</p>
  
  <pre><code>serverMiddleware: [
      { path: '/api', handler: '~/server/routesIndex.js' },
    ],
  </code></pre>
  
  <p>Doing so results in an error from <strong>fastify.register</strong>:</p>
  
  <pre><code>(node:10441) UnhandledPromiseRejectionWarning: TypeError: fastify.register is not a function
      at routes (/media/srv/testingNuxt/server/routesIndex.js:4:13)
      at call (/media/srv/testingNuxt/node_modules/connect/index.js:239:7)
      at next (/media/srv/testingNuxt/node_modules/connect/index.js:183:5)
      at next (/media/srv/testingNuxt/node_modules/connect/index.js:161:14)
      at WebpackBundler.middleware (/media/srv/testingNuxt/node_modules/@nuxt/webpack/dist/webpack.js:5430:5)
  (node:10441) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
  (node:10441) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  </code></pre>
  
  <p>How can I add api routes using Fastify and Nuxt?
  I want to avoid creating a second server on port 3001 just for the api.</p>