Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node): listen on 0.0.0.0 if server.host is set to true #10282

Merged
merged 8 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smooth-singers-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/node": patch
---

Modified node package to properly assign server.host option set to `true` to listen on all network interfaces. Added test to test this functionality.
SatanshuMishra marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 9 additions & 3 deletions packages/integrations/node/src/standalone.ts
Original file line number Diff line number Diff line change
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: string | boolean): string => {
SatanshuMishra marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
@@ -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);
});
});
Loading