Skip to content

Commit 307c8a4

Browse files
authored
Update example to use npm packages via package.json (#525)
## Summary - Replace deprecated `deno.land/std@0.77.0` HTTP server with a modern example using npm packages - Add `package.json` with express, chalk, and lodash-es to showcase Deno's npm compatibility - Remove unused `deps.ts` - Dockerfile caches `package.json` + `deno install` as a separate layer for efficient rebuilds - Move `USER deno` after dependency install to avoid permission issues with `node_modules` Closes #424
1 parent 8e81679 commit 307c8a4

5 files changed

Lines changed: 30 additions & 19 deletions

File tree

example/Dockerfile

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ EXPOSE 1993
55

66
WORKDIR /app
77

8-
# Prefer not to run as root.
9-
USER deno
10-
11-
# Cache the dependencies as a layer (the following two steps are re-run only when deps.ts is modified).
12-
# Ideally cache deps.ts will download and compile _all_ external files used in main.ts.
13-
COPY deps.ts .
14-
RUN deno install --entrypoint deps.ts
8+
# Cache dependencies as a layer (re-run only when package.json changes).
9+
COPY package.json .
10+
RUN deno install
1511

1612
# These steps will be re-run upon each file change in your working directory:
1713
COPY . .
1814
# Compile the main app so that it doesn't need to be compiled each startup/entry.
1915
RUN deno cache main.ts
2016

21-
CMD ["run", "--allow-net", "main.ts"]
17+
# Prefer not to run as root.
18+
USER deno
19+
20+
CMD ["run", "--allow-net", "--allow-read", "--allow-env", "main.ts"]

example/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
This example demonstrates using Deno with npm packages via `package.json`.
2+
13
The CMD line in the Dockerfile determines parameters passed to Deno.
24

35
```
4-
CMD ["run", "--allow-net", "main.ts"]
6+
CMD ["run", "--allow-net", "--allow-read", "--allow-env", "main.ts"]
57
```
68

79
Note: That the listen port (1993), must match with the EXPOSED port in the
@@ -11,12 +13,12 @@ Run using the build and run commands (you may want to use a more descriptive
1113
name than `app`):
1214

1315
```sh
14-
$ docker build -t app . && docker run -it --init -p 1993:1993 app
16+
$ docker build -t app . && docker run -it -p 1993:1993 app
1517
```
1618

1719
In another terminal or browser you can access:
1820

1921
```sh
2022
$ curl localhost:1993
21-
Hello World
23+
HELLO WORLD
2224
```

example/deps.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

example/main.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { serve } from "./deps.ts";
1+
import express from "express";
2+
import chalk from "chalk";
3+
import { upperCase } from "lodash-es";
24

35
const PORT = 1993;
4-
const s = serve(`0.0.0.0:${PORT}`);
5-
const body = new TextEncoder().encode("Hello World\n");
6+
const app = express();
67

7-
console.log(`Server started on port ${PORT}`);
8-
for await (const req of s) {
9-
req.respond({ body });
10-
}
8+
app.get("/", (_req, res) => {
9+
res.send(upperCase("Hello World") + "\n");
10+
});
11+
12+
app.listen(PORT, "0.0.0.0", () => {
13+
console.log(chalk.green(`Server started on port ${PORT}`));
14+
});

example/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": {
3+
"express": "^4.21.0",
4+
"chalk": "^5.4.0",
5+
"lodash-es": "^4.17.21"
6+
}
7+
}

0 commit comments

Comments
 (0)