From 12236dbc06e1e43618b61d180020a67cb31499f8 Mon Sep 17 00:00:00 2001 From: Scttpr Date: Thu, 17 Nov 2022 16:48:20 +0100 Subject: [PATCH] feat: provide HOST env variable at runtime (#5421) * feat: provide HOST env variable at runtime * doc: add change to documentation * Update documentation according to suggestions Co-authored-by: Chris Swithinbank * fix: empty string is considered as undefined Co-authored-by: Chris Swithinbank --- .changeset/violet-buckets-repeat.md | 5 +++++ packages/integrations/node/README.md | 9 +++++++++ packages/integrations/node/src/standalone.ts | 3 ++- 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 .changeset/violet-buckets-repeat.md diff --git a/.changeset/violet-buckets-repeat.md b/.changeset/violet-buckets-repeat.md new file mode 100644 index 000000000..ed68113e1 --- /dev/null +++ b/.changeset/violet-buckets-repeat.md @@ -0,0 +1,5 @@ +--- +'@astrojs/node': minor +--- + +Allow HOST env variable to be provided at runtime diff --git a/packages/integrations/node/README.md b/packages/integrations/node/README.md index be22cee8c..e0c8ea67f 100644 --- a/packages/integrations/node/README.md +++ b/packages/integrations/node/README.md @@ -109,6 +109,15 @@ node ./dist/server/entry.mjs For standalone mode the server handles file servering in addition to the page and API routes. + +#### Custom host and port + +You can override the host and port the standalone server runs on by passing them as environment variables at runtime: + +```shell +HOST=0.0.0.0 PORT=3000 node ./dist/server/entry.mjs +``` + #### HTTPS By default the standalone server uses HTTP. This works well if you have a proxy server in front of it that does HTTPS. If you need the standalone server to run HTTPS itself you need to provide your SSL key and certificate. diff --git a/packages/integrations/node/src/standalone.ts b/packages/integrations/node/src/standalone.ts index 5ec2455ee..1a5ab399e 100644 --- a/packages/integrations/node/src/standalone.ts +++ b/packages/integrations/node/src/standalone.ts @@ -39,7 +39,8 @@ export default function startServer(app: NodeApp, options: Options) { const { client } = resolvePaths(options); const handler = middleware(app); - const host = getResolvedHostForHttpServer(options.host); + // Allow to provide host value at runtime + const host = getResolvedHostForHttpServer(process.env.HOST !== undefined && process.env.HOST !== '' ? process.env.HOST : options.host); const server = createServer( { client,