2021-06-14 12:31:30 +00:00
# Node.js function
2021-10-11 17:18:38 +00:00
Discover the Node.js function preset with Nitro to attach Nuxt as a middleware to any Node.js server.
2021-06-14 12:31:30 +00:00
2021-10-11 17:18:38 +00:00
::list
2021-10-29 11:26:01 +00:00
2021-10-11 17:18:38 +00:00
- Compatible with many Node.js servers
2021-11-21 12:31:44 +00:00
- Drop-in usage with express or native HTTP server
2021-10-11 17:18:38 +00:00
- Loads only the chunks required to render the request for optimal cold start timing
::
2021-06-14 12:31:30 +00:00
2021-10-11 17:18:38 +00:00
::alert{icon=IconPresets}
2022-04-06 05:56:08 +00:00
Back to [presets list ](/guide/deployment/presets ).
2021-10-11 17:18:38 +00:00
::
2021-06-14 12:31:30 +00:00
2021-10-11 17:18:38 +00:00
## Usage
2022-04-06 05:56:08 +00:00
You can use the [Nuxt config ](/guide/directory-structure/nuxt.config ) to explicitly set the preset to use:
2021-10-11 17:18:38 +00:00
2021-10-29 11:26:01 +00:00
```js [nuxt.config.js|ts]
2021-10-11 17:18:38 +00:00
export default {
nitro: {
preset: 'node'
}
}
```
Or directly use the `NITRO_PRESET` environment variable when running `nuxt build` :
```bash
NITRO_PRESET=node npx nuxt build
```
2021-10-29 11:26:01 +00:00
## Entry point
2021-10-11 17:18:38 +00:00
2021-10-29 11:26:01 +00:00
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 ](https://expressjs.com/ ), [h3 ](https://github.com/unjs/h3 ), etc.
2021-10-11 17:18:38 +00:00
::alert{type=warning}
2021-06-14 12:31:30 +00:00
It is not recommended to use this preset directly, and particularly not with a 3rd-party server.
2021-10-11 17:18:38 +00:00
::
2021-06-14 12:31:30 +00:00
2021-10-11 17:18:38 +00:00
## Example
2021-06-14 12:31:30 +00:00
2021-10-11 17:18:38 +00:00
### Express middleware
2021-06-14 12:31:30 +00:00
2021-10-29 11:26:01 +00:00
```js
2021-06-14 12:31:30 +00:00
import express from 'express'
import handler from './.output/server'
const app = express()
app.use(handler)
app.listen(3000)
```
2021-10-11 17:18:38 +00:00
### Node server
2021-06-14 12:31:30 +00:00
2021-10-29 11:26:01 +00:00
```js
2022-04-15 15:19:05 +00:00
import { createServer } from 'node:http'
2021-06-14 12:31:30 +00:00
import handler from './.output/server'
const server = createServer(handler)
server.listen(8080)
```