Skip to content

Commit 721ead1

Browse files
Filmbostock
andauthored
review port documentation (#1363)
* review port documentation; add EACCES as an acceptable error to continue port hopping; avoid out of memory error if no registered port is reachable * Update src/preview.ts * MAX_PORT * Update src/preview.ts Co-authored-by: Mike Bostock <[email protected]> * Update docs/getting-started.md Co-authored-by: Mike Bostock <[email protected]> * Update docs/getting-started.md Co-authored-by: Mike Bostock <[email protected]> * Update docs/getting-started.md Co-authored-by: Mike Bostock <[email protected]> * more sh --------- Co-authored-by: Mike Bostock <[email protected]>
1 parent bec1a11 commit 721ead1

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

docs/getting-started.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,21 @@ You should see something like this:
164164
↳ <u><a href="http://127.0.0.1:3000/" style="color: inherit;">http://127.0.0.1:3000/</a></u></pre>
165165

166166
<div class="note">
167-
<p>If port 3000 is in use, the preview server will choose the next available port, so your actual port may vary. To specify port 4321 (and similarly for any other port), use <nobr><code>--port 4321</code></nobr>.</p>
168-
<p>For security, the preview server is by default only accessible on your local machine using the <a href="https://en.wikipedia.org/wiki/Localhost">loopback address</a> 127.0.0.1. To allow remote connections, use <nobr><code>--host 0.0.0.0</code></nobr>.</p>
167+
168+
If port 3000 is in use, the preview server will choose the next available port, so your actual port may vary. To specify a different port, use <nobr>`--port`</nobr>. For example, to run the preview server with npm on port 4321:
169+
170+
```sh
171+
npm run dev -- --port 4321
172+
```
173+
174+
For security, the preview server is by default only accessible on your local machine using the [loopback address](https://en.wikipedia.org/wiki/Localhost) 127.0.0.1. To allow remote connections, use <nobr>`--host 0.0.0.0`</nobr>.
175+
176+
Port numbers below 1024 may need admin privileges. For example, to share your preview server on the default HTTP port 80:
177+
178+
```sh
179+
sudo npm run dev -- --host 0.0.0.0 --port 80
180+
```
181+
169182
</div>
170183

171184
Now visit <http://127.0.0.1:3000> in your browser, which should look like:

src/preview.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ export class PreviewServer {
7777
Telemetry.record({event: "preview", step: "start"});
7878
const server = createServer();
7979
if (port === undefined) {
80-
for (port = 3000; true; ++port) {
80+
const MAX_PORT = 49152; // https://en.wikipedia.org/wiki/Registered_port
81+
for (port = 3000; port < MAX_PORT; ++port) {
8182
try {
8283
await new Promise<void>((resolve, reject) => {
8384
server.once("error", reject);
@@ -88,6 +89,7 @@ export class PreviewServer {
8889
if (!isSystemError(error) || error.code !== "EADDRINUSE") throw error;
8990
}
9091
}
92+
if (port === MAX_PORT) throw new Error(`Couldn’t connect to any port on ${hostname}`);
9193
} else {
9294
await new Promise<void>((resolve) => server.listen(port, hostname, resolve));
9395
}

0 commit comments

Comments
 (0)