diff --git a/src/presets/node/runtime/node-cluster.ts b/src/presets/node/runtime/node-cluster.ts index 1b6cfe5499..ec3b95c781 100644 --- a/src/presets/node/runtime/node-cluster.ts +++ b/src/presets/node/runtime/node-cluster.ts @@ -62,11 +62,13 @@ function runMaster() { } function runWorker() { - import("./node-server").catch((error) => { - console.error(error); - // eslint-disable-next-line unicorn/no-process-exit - process.exit(1); - }); + import("./node-server") + .then(({ serverWorker }) => serverWorker()) + .catch((error) => { + console.error(error); + // eslint-disable-next-line unicorn/no-process-exit + process.exit(1); + }); } // Trap unhandled errors diff --git a/src/presets/node/runtime/node-server.ts b/src/presets/node/runtime/node-server.ts index a8737bda74..2087647e66 100644 --- a/src/presets/node/runtime/node-server.ts +++ b/src/presets/node/runtime/node-server.ts @@ -12,60 +12,62 @@ import { trapUnhandledNodeErrors, } from "nitropack/runtime/internal"; -const cert = process.env.NITRO_SSL_CERT; -const key = process.env.NITRO_SSL_KEY; +export const serverWorker = async () => { + const cert = process.env.NITRO_SSL_CERT; + const key = process.env.NITRO_SSL_KEY; -const nitroApp = useNitroApp(); + const nitroApp = useNitroApp(); -const server = - cert && key - ? new HttpsServer({ key, cert }, toNodeListener(nitroApp.h3App)) - : new HttpServer(toNodeListener(nitroApp.h3App)); + const server = + cert && key + ? new HttpsServer({ key, cert }, toNodeListener(nitroApp.h3App)) + : new HttpServer(toNodeListener(nitroApp.h3App)); -const port = (destr(process.env.NITRO_PORT || process.env.PORT) || - 3000) as number; -const host = process.env.NITRO_HOST || process.env.HOST; + const port = (destr(process.env.NITRO_PORT || process.env.PORT) || + 3000) as number; + const host = process.env.NITRO_HOST || process.env.HOST; -const path = process.env.NITRO_UNIX_SOCKET; + const path = process.env.NITRO_UNIX_SOCKET; -// @ts-ignore -const listener = server.listen(path ? { path } : { port, host }, (err) => { - if (err) { - console.error(err); - // eslint-disable-next-line unicorn/no-process-exit - process.exit(1); - } - const protocol = cert && key ? "https" : "http"; - const addressInfo = listener.address() as AddressInfo; - if (typeof addressInfo === "string") { - console.log(`Listening on unix socket ${addressInfo}`); - return; - } - const baseURL = (useRuntimeConfig().app.baseURL || "").replace(/\/$/, ""); - const url = `${protocol}://${ - addressInfo.family === "IPv6" - ? `[${addressInfo.address}]` - : addressInfo.address - }:${addressInfo.port}${baseURL}`; - console.log(`Listening on ${url}`); -}); + // @ts-ignore + const listener = server.listen(path ? { path } : { port, host }, (err) => { + if (err) { + console.error(err); + // eslint-disable-next-line unicorn/no-process-exit + process.exit(1); + } + const protocol = cert && key ? "https" : "http"; + const addressInfo = listener.address() as AddressInfo; + if (typeof addressInfo === "string") { + console.log(`Listening on unix socket ${addressInfo}`); + return; + } + const baseURL = (useRuntimeConfig().app.baseURL || "").replace(/\/$/, ""); + const url = `${protocol}://${ + addressInfo.family === "IPv6" + ? `[${addressInfo.address}]` + : addressInfo.address + }:${addressInfo.port}${baseURL}`; + console.log(`Listening on ${url}`); + }); -// Trap unhandled errors -trapUnhandledNodeErrors(); + // Trap unhandled errors + trapUnhandledNodeErrors(); -// Graceful shutdown -setupGracefulShutdown(listener, nitroApp); + // Graceful shutdown + setupGracefulShutdown(listener, nitroApp); -// Websocket support -// https://crossws.unjs.io/adapters/node -if (import.meta._websocket) { - const { handleUpgrade } = wsAdapter(nitroApp.h3App.websocket); - server.on("upgrade", handleUpgrade); -} + // Websocket support + // https://crossws.unjs.io/adapters/node + if (import.meta._websocket) { + const { handleUpgrade } = wsAdapter(nitroApp.h3App.websocket); + server.on("upgrade", handleUpgrade); + } -// Scheduled tasks -if (import.meta._tasks) { - startScheduleRunner(); -} + // Scheduled tasks + if (import.meta._tasks) { + startScheduleRunner(); + } +}; export default {};