The server property

Nuxt let you define the server connection variables for your application inside nuxt.config.js.


  • Type: Object

Basic example:

nuxt.config.js

  1. export default {
  2. server: {
  3. port: 8000, // default: 3000
  4. host: '0.0.0.0', // default: localhost,
  5. timing: false
  6. }
  7. }

This lets you specify the host and port for your Nuxt server instance.

Example using HTTPS configuration

nuxt.config.js

  1. import path from 'path'
  2. import fs from 'fs'
  3. export default {
  4. server: {
  5. https: {
  6. key: fs.readFileSync(path.resolve(__dirname, 'server.key')),
  7. cert: fs.readFileSync(path.resolve(__dirname, 'server.crt'))
  8. }
  9. }
  10. }

You can find additional information on creating server keys and certificates on localhost on certificates for localhost article.

Example using sockets configuration

nuxt.config.js

  1. export default {
  2. server: {
  3. socket: '/tmp/nuxt.socket'
  4. }
  5. }

timing

  • Type: Object or Boolean
  • Default: false

Enabling the server.timing option adds a middleware to measure the time elapsed during server-side rendering and adds it to the headers as ‘Server-Timing’

Example using timing configuration

server.timing can be an object for providing options. Currently, only total is supported (which directly tracks the whole time spent on server-side rendering)

nuxt.config.js

  1. export default {
  2. server: {
  3. timing: {
  4. total: true
  5. }
  6. }
  7. }

Using timing API

The timing API is also injected into the response on server-side when server.time is enabled.

Syntax

  1. res.timing.start(name, description)
  2. res.timing.end(name)

Example using timing in serverMiddleware

  1. export default function (req, res, next) {
  2. res.timing.start('midd', 'Middleware timing description')
  3. // server side operation..
  4. // ...
  5. res.timing.end('midd')
  6. next()
  7. }

Then server-timing head will be included in response header like:

  1. Server-Timing: midd;desc="Middleware timing description";dur=2.4

Please refer to Server-Timing MDN for more details.