nuxt.render(req, res)
使用 nuxt.render 可以将 Nuxt 作为 Node.js 服务器的中间件使用。
使用 Express 的示例:
const { loadNuxt, build } = require('nuxt')
const app = require('express')()
const isDev = process.env.NODE_ENV !== 'production'
const port = process.env.PORT || 3000
async function start() {
// 获取 Nuxt 实例
const nuxt = await loadNuxt(isDev ? 'dev' : 'start')
// 用 Nuxt 渲染所有路由
app.use(nuxt.render)
// 仅在开发模式下构建(带热重载)
if (isDev) {
build(nuxt)
}
// 监听服务器
app.listen(port, '0.0.0.0')
console.log('Server listening on `localhost:' + port + '`.')
}
start()
建议在中间件末尾调用
nuxt.render,因为 nuxt.render 会处理 Web 应用程序的渲染,不会调用 next()。
Edit this page on GitHub
Updated at Tue, Apr 14, 2026