Node.js function

Discover the Node.js function preset with Nitro to attach Nuxt as a middleware to any Node.js server.

Node.js function - 图1 Compatible with many Node.js servers

Node.js function - 图2 Drop-in usage with express or native HTTP server

Node.js function - 图3 Loads only the chunks required to render the request for optimal cold start timing

Node.js function - 图4

Back to presets list.

Usage

You can use the Nuxt config to explicitly set the preset to use:

nuxt.config.js|ts

  1. export default {
  2. nitro: {
  3. preset: 'node'
  4. }
  5. }

Or directly use the NITRO_PRESET environment variable when running nuxt build:

  1. NITRO_PRESET=node npx nuxt build

Entry point

When running nuxt build with the Node preset, the result will be an entry point exporting a function with the familiar (req, res) => {} signature used in express, h3, etc.

It is not recommended to use this preset directly, and particularly not with a 3rd-party server.

Example

Express middleware

  1. import express from 'express'
  2. import handler from './.output/server'
  3. const app = express()
  4. app.use(handler)
  5. app.listen(3000)

Node server

  1. import { createServer } from 'node:http'
  2. import handler from './.output/server'
  3. const server = createServer(handler)
  4. server.listen(8080)