Skip to content

Commit

Permalink
Support passing the port via the command line
Browse files Browse the repository at this point in the history
  • Loading branch information
jradcliff committed Feb 22, 2024
1 parent a0d5570 commit 02f35cc
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 20 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ Once you install Deno, run the local web server with the following command:
deno task start
```

Then, open [http://localhost](http://localhost) in your web browser.
Then, open <http://localhost> in your web browser.

By default, the server listens on port 80. If that port is in use or is
otherwise unavailable, override the port using the `--port` argument. For
example, you could run the server on port 8000 as follows:

```
deno task start --port 8000
```

Then, open <http://localhost:8000> in your web browser.

# Join our community

Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"tasks": {
"start": "deno run -A --watch=src/public/ src/index.ts"
"start": "deno run --allow-net --allow-read --watch=src/public/ src/index.ts"
},
"fmt": {
"files": {
Expand Down
4 changes: 4 additions & 0 deletions src/deno.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"version": "2",
"remote": {
"https://deno.land/[email protected]/assert/assert.ts": "9a97dad6d98c238938e7540736b826440ad8c1c1e54430ca4c4e623e585607ee",
"https://deno.land/[email protected]/assert/assertion_error.ts": "4d0bde9b374dfbcbe8ac23f54f567b77024fb67dbb1906a852d67fe050d42f56",
"https://deno.land/[email protected]/cli/parse_args.ts": "9bea02050b3f302e706871ff87ecfa3ad82cc34249adbe0dcddfaac75bdb48ff",
"https://deno.land/[email protected]/flags/mod.ts": "567a34800a33e701942cb1726dd7e627a76e4681c33ce7346ac85cf90f691a8e",
"https://deno.land/[email protected]/_util/assert.ts": "e1f76e77c5ccb5a8e0dbbbe6cce3a56d2556c8cb5a9a8802fc9565af72462149",
"https://deno.land/[email protected]/fs/_util.ts": "68508c05d5a02678179a02beabf2b3eac5b16c5a9cbd1c272686d8101bb2679d",
"https://deno.land/[email protected]/fs/copy.ts": "b562e8f482cb8459bb011cbd769769bbdb4f6bc966effd277c06edbdbe41b72e",
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { configure, renderFile } from "https://deno.land/x/[email protected]/mod.ts";
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";

const __dirname = new URL(".", import.meta.url).pathname;

Expand All @@ -9,10 +10,14 @@ const viewPath = [
];

configure({ views: viewPath });
const flags = parse(Deno.args, {
string: [ "port" ],
default: { port: 80 },
});

const server = Deno.listen({ port: 80 });
const server = Deno.listen({ port: flags.port });

console.log("File server running on http://localhost:80/");
console.log("File server running on http://localhost:" + flags.port + "/");

for await (const conn of server) {
handleHttp(conn).catch(console.error);
Expand Down
12 changes: 12 additions & 0 deletions tutorials/1-initial-setup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ command from this folder
deno run --allow-net --allow-read ./src/index.ts
```

Then, open <http://localhost> in your web browser.

By default, the server listens on port 80. If that port is in use or is
otherwise unavailable, override the port using the `--port` argument. For
example, you could run the server on port 8000 as follows:

```
deno run --allow-net --allow-read ./src/index.ts --port 8000
```

Then, open <http://localhost:8000> in your web browser.

# Join Our Community

💬 [Join](https://discord.gg/65mah7ZZsG) the official GA Discord server\
Expand Down
16 changes: 12 additions & 4 deletions tutorials/1-initial-setup/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const server = Deno.listen({ port: 80 });
console.log("File server running on http://localhost:80/index.html");
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";

const flags = parse(Deno.args, {
string: [ "port" ],
default: { port: 80 },
});

const server = Deno.listen({ port: flags.port });
console.log("File server running on http://localhost:" + flags.port + "/");

const __dirname = new URL(".", import.meta.url).pathname;

for await (const conn of server) {
Expand All @@ -9,7 +17,7 @@ for await (const conn of server) {
async function handleHttp(conn: Deno.Conn) {
const httpConn = Deno.serveHttp(conn);
for await (const requestEvent of httpConn) {

const url = new URL(requestEvent.request.url);
const filepath = decodeURIComponent(url.pathname);

Expand All @@ -27,4 +35,4 @@ async function handleHttp(conn: Deno.Conn) {
const response = new Response(readableStream);
await requestEvent.respondWith(response);
}
}
}
12 changes: 12 additions & 0 deletions tutorials/2-events/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ command from this folder [/tutorials/2-events](/tutorials/2-events):
deno run --allow-net --allow-read ./src/index.ts
```

Then, open <http://localhost> in your web browser.

By default, the server listens on port 80. If that port is in use or is
otherwise unavailable, override the port using the `--port` argument. For
example, you could run the server on port 8000 as follows:

```
deno run --allow-net --allow-read ./src/index.ts --port 8000
```

Then, open <http://localhost:8000> in your web browser.

# Join Our Community

💬 [Join](https://discord.gg/65mah7ZZsG) the official GA Discord server\
Expand Down
16 changes: 12 additions & 4 deletions tutorials/2-events/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const server = Deno.listen({ port: 80 });
console.log("File server running on http://localhost:80/index.html");
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";

const flags = parse(Deno.args, {
string: [ "port" ],
default: { port: 80 },
});

const server = Deno.listen({ port: flags.port });
console.log("File server running on http://localhost:" + flags.port + "/");

const __dirname = new URL(".", import.meta.url).pathname;

for await (const conn of server) {
Expand All @@ -9,7 +17,7 @@ for await (const conn of server) {
async function handleHttp(conn: Deno.Conn) {
const httpConn = Deno.serveHttp(conn);
for await (const requestEvent of httpConn) {

const url = new URL(requestEvent.request.url);
let filepath = decodeURIComponent(url.pathname);
if(filepath === "/") {
Expand All @@ -30,4 +38,4 @@ async function handleHttp(conn: Deno.Conn) {
const response = new Response(readableStream);
await requestEvent.respondWith(response);
}
}
}
12 changes: 12 additions & 0 deletions tutorials/3-custom-events/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ command from this folder [/tutorials/3-events](/tutorials/3-custom-events):
deno run --allow-net --allow-read ./src/index.ts
```

Then, open <http://localhost> in your web browser.

By default, the server listens on port 80. If that port is in use or is
otherwise unavailable, override the port using the `--port` argument. For
example, you could run the server on port 8000 as follows:

```
deno run --allow-net --allow-read ./src/index.ts --port 8000
```

Then, open <http://localhost:8000> in your web browser.

# Join Our Community

💬 [Join](https://discord.gg/65mah7ZZsG) the official GA Discord server\
Expand Down
16 changes: 12 additions & 4 deletions tutorials/3-custom-events/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const server = Deno.listen({ port: 80 });
console.log("File server running on http://localhost:80/index.html");
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";

const flags = parse(Deno.args, {
string: [ "port" ],
default: { port: 80 },
});

const server = Deno.listen({ port: flags.port });
console.log("File server running on http://localhost:" + flags.port + "/");

const __dirname = new URL(".", import.meta.url).pathname;

for await (const conn of server) {
Expand All @@ -9,7 +17,7 @@ for await (const conn of server) {
async function handleHttp(conn: Deno.Conn) {
const httpConn = Deno.serveHttp(conn);
for await (const requestEvent of httpConn) {

const url = new URL(requestEvent.request.url);
let filepath = decodeURIComponent(url.pathname);
if(filepath === "/") {
Expand All @@ -32,4 +40,4 @@ async function handleHttp(conn: Deno.Conn) {
const response = new Response(readableStream);
await requestEvent.respondWith(response);
}
}
}
12 changes: 11 additions & 1 deletion tutorials/4-consent-mode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ Once you install Deno, run the local web server with the following command:
deno task start
```

Then, open [http://localhost](http://localhost) in your web browser.
Then, open <http://localhost> in your web browser.

By default, the server listens on port 80. If that port is in use or is
otherwise unavailable, override the port using the `--port` argument. For
example, you could run the server on port 8000 as follows:

```
deno task start --port 8000
```

Then, open <http://localhost:8000> in your web browser.

# Join our community

Expand Down
2 changes: 1 addition & 1 deletion tutorials/4-consent-mode/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"tasks": {
"start": "deno run -A --watch=src/public/ src/index.ts"
"start": "deno run --allow-net --allow-read --watch=src/public/ src/index.ts"
},
"fmt": {
"files": {
Expand Down
9 changes: 7 additions & 2 deletions tutorials/4-consent-mode/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { configure, renderFile } from "https://deno.land/x/[email protected]/mod.ts";
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";

const __dirname = new URL(".", import.meta.url).pathname;

Expand All @@ -10,9 +11,13 @@ const viewPath = [

configure({ views: viewPath });

const server = Deno.listen({ port: 80 });
const flags = parse(Deno.args, {
string: [ "port" ],
default: { port: 80 },
});

console.log("File server running on http://localhost:80/");
const server = Deno.listen({ port: flags.port });
console.log("File server running on http://localhost:" + flags.port + "/");

for await (const conn of server) {
handleHttp(conn).catch(console.error);
Expand Down

0 comments on commit 02f35cc

Please sign in to comment.