server 属性
Nuxt 允许在 nuxt.config.js 中定义应用程序服务器连接的变量。
-
类型:
Object
基本示例:
nuxt.config.js
export default {
server: {
port: 8000, // 默认: 3000
host: '0.0.0.0', // 默认: localhost,
timing: false
}
}
这样可以指定 Nuxt 服务器实例的 host 和 port 。
使用 HTTPS 配置的示例
nuxt.config.js
import path from 'path'
import fs from 'fs'
export default {
server: {
https: {
key: fs.readFileSync(path.resolve(__dirname, 'server.key')),
cert: fs.readFileSync(path.resolve(__dirname, 'server.crt'))
}
}
}
有关在 localhost 上创建服务器密钥和证书的方法,请参阅 certificates for localhost 文章。
使用 sockets 配置的示例
nuxt.config.js
export default {
server: {
socket: '/tmp/nuxt.socket'
}
}
timing
-
类型:
Object或Boolean -
默认值:
false
启用 server.timing 选项后,会添加一个中间件来测量服务端渲染期间经过的时间,并将其作为 'Server-Timing' 添加到头中。
使用 timing 配置的示例
server.timing 是一个提供选项的对象,目前只支持 total(直接追踪服务端渲染花费的所有时间)。
nuxt.config.js
export default {
server: {
timing: {
total: true
}
}
}
使用 timing API
启用 server.time 时,timing API 也会注入到服务端的 response 中。
语法
res.timing.start(name, description)
res.timing.end(name)
在服务端中间件中使用 timing 的示例
export default function (req, res, next) {
res.timing.start('midd', 'Middleware timing description')
// 服务端处理...
// ...
res.timing.end('midd')
next()
}
然后 server-timing 会包含在响应头中,如下所示:
Server-Timing: midd;desc="Middleware timing description";dur=2.4
详情请参阅 MDN 的 Server-Timing 。
Edit this page on GitHub
Updated at Tue, Apr 14, 2026