Node.js
预设:node_server
¥Preset: node_server
Node.js 是 Nitro 生产构建的默认输出预设,并且 Nitro 原生支持 Node.js 运行时。
¥Node.js is the default nitro output preset for production builds and Nitro has native Node.js runtime support.
使用 nitro CLI 构建项目:
¥Build project using nitro CLI:
nitro build
使用 Node 服务器预设运行 nitro build
时,结果将是一个启动即用型 Node 服务器的入口点。尝试输出:
¥When running nitro build
with the Node server preset, the result will be an entry point that launches a ready-to-run Node server. To try output:
$ node .output/server/index.mjs
Listening on http://localhost:3000
你现在可以将完全独立的 .output
目录部署到你选择的主机上。
¥You can now deploy fully standalone .output
directory to the hosting of your choice.
环境变量
¥Environment Variables
你可以使用以下环境变量自定义服务器行为:
¥You can customize server behavior using following environment variables:
NITRO_PORT
或PORT
(默认为3000
)NITRO_HOST
或HOST
NITRO_UNIX_SOCKET
- 如果提供了(所需套接字文件的路径),则服务将通过提供的 UNIX 套接字提供服务。NITRO_SSL_CERT
和NITRO_SSL_KEY
- 如果两者都存在,这将以 HTTPS 模式启动服务器。在绝大多数情况下,除了测试之外,不应将其用于其他用途,并且 Nitro 服务器应在反向代理(例如 nginx 或 Cloudflare)之后运行,这些代理会终止 SSL。NITRO_SHUTDOWN_DISABLED
- 设置为'true'
时禁用优雅关机功能。如果设置为'true'
,则会绕过正常关闭以加快开发进程。默认为'false'
。NITRO_SHUTDOWN_SIGNALS
- 允许你指定应处理的信号。每个信号应以空格分隔。默认为'SIGINT SIGTERM'
。NITRO_SHUTDOWN_TIMEOUT
- 设置强制关闭之前的时间(以毫秒为单位)。默认为'30000'
毫秒。NITRO_SHUTDOWN_FORCE
- 设置为 true 时,会在关闭过程结束时触发process.exit()
。如果设置为'false'
,该进程将直接清除事件循环。默认为'true'
。
集群模式
¥Cluster mode
预设:node_cluster
¥Preset: node_cluster
为了提高性能并利用多核处理能力,你可以使用集群预设。
¥For more performance and leveraging multi-core handling, you can use cluster preset.
环境变量
¥Environment Variables
除了 node_server
预设中的环境变量外,你还可以自定义行为:
¥In addition to environment variables from the node_server
preset, you can customize behavior:
NITRO_CLUSTER_WORKERS
:集群工作线程数量(默认值为可用 CPU 核心数)
处理程序(高级)
¥Handler (advanced)
预设:node
¥Preset: node
Nitro 还有一个更底层的预设,可以直接导出一个带有 (req, res) => {}
签名的函数,可用于中间件和自定义服务器。
¥Nitro also has a more low-level preset that directly exports a function with (req, res) => {}
signature usable for middleware and custom servers.
使用 Node 预设运行 nitro build
时,结果将是一个导出带有 (req, res) => {}
签名的函数的入口点。
¥When running nitro build
with the Node preset, the result will be an entry point exporting a function with the (req, res) => {}
signature.
示例:
¥Example:
import { createServer } from 'node:http'
import { listener } from './.output/server'
const server = createServer(listener)
server.listen(8080)