Node
The Node adapter allows you to deploy your Vite project to many hosts. This guide doesn’t cover the specifics of each host but shows general purpose implementations.
Simple HTTP server
Section titled “Simple HTTP server”You need to handle serving static, for example using sirv:
import * as http from "node:http";import { fileURLToPath } from "node:url";import sirv from "sirv";import mod from "./handler";
const server = http.createServer((req, res) => sirv(fileURLToPath(new URL("../client/", import.meta.url)), { setHeaders: (res, pathname) => { if (pathname.startsWith("/assets/")) { res.setHeader("Cache-Control", "public, max-age=31536000, immutable"); } }, })(req, res, () => { mod.handler(req, res); }),);
server.listen(3000, () => { console.log("Ready at http://localhost:3000");});Cluster
Section titled “Cluster”import mod from "./handler";import { toNodeHandler } from "srvx/node";import * as http from "node:http";import sirv from "sirv";import { fileURLToPath } from "node:url";import cluster from "node:cluster";import os from "node:os";
if (cluster.isPrimary) { console.log(`Primary ${process.pid} is running`); const numberOfWorkers = os.cpus().length || 1; for (let i = 0; i < numberOfWorkers; i++) { cluster.fork({ WORKER_ID: i + 1, }); }
cluster.on("exit", (worker, _code, _signal) => { console.log(`worker ${worker.process.pid} died`); });} else { const server = http.createServer((req, res) => sirv(fileURLToPath(new URL("../client/", import.meta.url)), { setHeaders: (res, pathname) => { if (pathname.startsWith("/assets/")) { res.setHeader("Cache-Control", "public, max-age=31536000, immutable"); } }, })(req, res, () => { // @ts-expect-error toNodeHandler(mod.fetch)(req, res); }), );
server.listen(3000, () => { console.log("Ready at http://localhost:3000"); });
console.log(`Worker ${process.pid} started`);}