Skip to content

Commit

Permalink
fix(node): listen on 0.0.0.0 if server.host is set to true (#10282)
Browse files Browse the repository at this point in the history
Co-authored-by: Arsh <69170106+lilnasy@users.noreply.github.com>
Co-authored-by: Kevin Zuniga Cuellar <46791833+kevinzunigacuellar@users.noreply.github.com>
Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
  • Loading branch information
4 people committed Mar 1, 2024
1 parent 560a593 commit b47dcaa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-singers-kiss.md
@@ -0,0 +1,5 @@
---
"@astrojs/node": patch
---

Fixes the `server.host` option to properly listen on all network interfaces when set to `true`
12 changes: 9 additions & 3 deletions packages/integrations/node/src/standalone.ts
Expand Up @@ -9,11 +9,17 @@ import { createAppHandler } from './serve-app.js';
import { createStaticHandler } from './serve-static.js';
import type { Options } from './types.js';

// Used to get Host Value at Runtime
export const hostOptions = (host: Options["host"]): string => {
if (typeof host === 'boolean') {
return host ? '0.0.0.0' : 'localhost';
}
return host;
};

export default function standalone(app: NodeApp, options: Options) {
const port = process.env.PORT ? Number(process.env.PORT) : options.port ?? 8080;
// Allow to provide host value at runtime
const hostOptions = typeof options.host === 'boolean' ? 'localhost' : options.host;
const host = process.env.HOST ?? hostOptions;
const host = process.env.HOST ?? hostOptions(options.host);
const handler = createStandaloneHandler(app, options);
const server = createServer(handler, host, port);
server.server.listen(port, host);
Expand Down
21 changes: 21 additions & 0 deletions packages/integrations/node/test/server-host.test.js
@@ -0,0 +1,21 @@
import { describe, it } from 'node:test';
import * as assert from 'node:assert/strict';
import { hostOptions } from '../dist/standalone.js';

describe('host', () => {
it('returns "0.0.0.0" when host is true', () => {
const options = { host: true };
assert.equal(hostOptions(options.host), '0.0.0.0');
});

it('returns "localhost" when host is false', () => {
const options = { host: false };
assert.equal(hostOptions(options.host), 'localhost');
});

it('returns the value of host when host is a string', () => {
const host = "1.1.1.1"
const options = { host };
assert.equal(hostOptions(options.host), host);
});
});

0 comments on commit b47dcaa

Please sign in to comment.