Skip to content

Commit

Permalink
[feat] allow node adapter to configure listen path (#2048)
Browse files Browse the repository at this point in the history
  • Loading branch information
matths authored Aug 12, 2021
1 parent da94af3 commit 4740c62
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-waves-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-node': patch
---

[feat] allow node adapter to configure listen path
7 changes: 5 additions & 2 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const pipe = promisify(pipeline);
* out?: string;
* precompress?: boolean;
* env?: {
* path?: string;
* host?: string;
* port?: string;
* };
Expand All @@ -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} */
Expand All @@ -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;`
);
Expand Down
9 changes: 6 additions & 3 deletions packages/adapter-node/src/index.js
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit 4740c62

Please sign in to comment.