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 <swithinbank@gmail.com>

* fix: empty string is considered as undefined

Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
This commit is contained in:
Scttpr 2022-11-17 16:48:20 +01:00 committed by GitHub
parent ff35b4759b
commit 12236dbc06
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/node': minor
---
Allow HOST env variable to be provided at runtime

View file

@ -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. 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 #### 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. 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.

View file

@ -39,7 +39,8 @@ export default function startServer(app: NodeApp, options: Options) {
const { client } = resolvePaths(options); const { client } = resolvePaths(options);
const handler = middleware(app); 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( const server = createServer(
{ {
client, client,