diff --git a/.changeset/eighty-waves-obey.md b/.changeset/eighty-waves-obey.md new file mode 100644 index 000000000000..1d5281fdfb60 --- /dev/null +++ b/.changeset/eighty-waves-obey.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-node': patch +--- + +[feat] allow node adapter to configure listen path diff --git a/packages/adapter-node/index.js b/packages/adapter-node/index.js index cfc3a9b702fa..8fcd96c274f5 100644 --- a/packages/adapter-node/index.js +++ b/packages/adapter-node/index.js @@ -25,6 +25,7 @@ const pipe = promisify(pipeline); * out?: string; * precompress?: boolean; * env?: { + * path?: string; * host?: string; * port?: string; * }; @@ -34,7 +35,7 @@ const pipe = promisify(pipeline); export default function ({ out = 'build', precompress, - env: { host: host_env = 'HOST', port: port_env = 'PORT' } = {}, + env: { path: path_env = 'SOCKET_PATH', host: host_env = 'HOST', port: port_env = 'PORT' } = {}, esbuild: esbuildConfig } = {}) { /** @type {import('@sveltejs/kit').Adapter} */ @@ -57,7 +58,9 @@ export default function ({ utils.copy(files, '.svelte-kit/node'); writeFileSync( '.svelte-kit/node/env.js', - `export const host = process.env[${JSON.stringify( + `export const path = process.env[${JSON.stringify( + path_env + )}] || false;\nexport const host = process.env[${JSON.stringify( host_env )}] || '0.0.0.0';\nexport const port = process.env[${JSON.stringify(port_env)}] || 3000;` ); diff --git a/packages/adapter-node/src/index.js b/packages/adapter-node/src/index.js index 7a3973be9776..b7d7c27dc5e4 100644 --- a/packages/adapter-node/src/index.js +++ b/packages/adapter-node/src/index.js @@ -1,12 +1,15 @@ // TODO hardcoding the relative location makes this brittle import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved -import { host, port } from './env.js'; // eslint-disable-line import/no-unresolved +import { path, host, port } from './env.js'; // eslint-disable-line import/no-unresolved import { createServer } from './server'; init(); -const instance = createServer({ render }).listen(port, host, () => { - console.log(`Listening on ${host}:${port}`); +const instance = createServer({ render }); + +const listenOpts = { path, host, port }; +instance.listen(listenOpts, () => { + console.log(`Listening on ${path ? path : host + ':' + port}`); }); export { instance };