API: The serverMiddleware Property

  • Type: Array
    • Items: String or Object or Function

Nuxt internally creates a connect instance that we can add our own custom middleware to. This allows us to register additional routes (typically /api routes) without need for an external server.

Because connect itself is a middleware, registered middleware will work with both nuxt start and also when used as a middleware with programmatic usages like express-template. Nuxt Modules can also provide serverMiddleware using this.addServerMiddleware()

Additional to them, we introduced a prefix option which defaults to true. It will add the router base to your server middlewares.

Example:

  • Server middleware path: /api
  • Router base: /admin
  • With prefix: true (default): /admin/api
  • With prefix: false: /api

serverMiddleware vs middleware!

Don't confuse it with routes middleware which are called before each route by Vue in Client Side or SSR. Middleware listed in the serverMiddleware property runs server-side before vue-server-renderer and can be used for server specific tasks like handling API requests or serving assets.

Usage

If middleware is String Nuxt.js will try to automatically resolve and require it.

Example (nuxt.config.js):

import serveStatic from 'serve-static'

export default {
  serverMiddleware: [
    // Will register redirect-ssl npm package
    'redirect-ssl',

    // Will register file from project api directory to handle /api/* requires
    { path: '/api', handler: '~/api/index.js' },

    // We can create custom instances too
    { path: '/static2', handler: serveStatic(__dirname + '/static2') }
  ]
}

HEADS UP! If you don't want middleware to register for all routes you have to use Object form with specific path, otherwise nuxt default handler won't work!

Custom Server Middleware

It is also possible to write custom middleware. For more information See Connect Docs.

Middleware (api/logger.js):

export default function (req, res, next) {
  // req is the Node.js http request object
  console.log(req.url)

  // res is the Node.js http response object

  // next is a function to call to invoke the next middleware
  // Don't forget to call next at the end if your middleware is not an endpoint!
  next()
}

Nuxt Config (nuxt.config.js):

serverMiddleware: [
  '~/api/logger'
]

Object Syntax

If your server middleware consists of a list of functions mapped to paths:

export default {
  serverMiddleware: [
    { path: '/a', handler: '~/api/a.js' },
    { path: '/b', handler: '~/api/b.js' },
    { path: '/c', handler: '~/api/c.js' },
  ]
}

You can alternatively pass an object to define them, as follows:

export default {
  serverMiddleware: {
    '/a': '~/api/a.js',
    '/b': '~/api/b.js',
    '/c': '~/api/c.js',
  }
}
server srcDir

Contributors

Caught a mistake or want to contribute to the documentation? Edit this page on GitHub!

Platinum Sponsors

Storyblok Support Us