Skip to content

Commit 769e98f

Browse files
Express tut update (#1538)
1 parent c2ab542 commit 769e98f

File tree

1 file changed

+75
-31
lines changed

1 file changed

+75
-31
lines changed

examples/tutorials/express.md

Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,37 @@ simple and unopinionated with a large ecosystem of middleware.
1313
This How To guide will show you how to create a simple API using Express and
1414
Deno.
1515

16-
[View source here.](https://github.com/denoland/examples/tree/main/with-express)
16+
[View source here.](https://github.com/denoland/tutorial-with-express)
1717

18-
## Create `main.ts`
18+
## Initialize a new deno project
1919

20-
Let's create `main.ts`:
20+
In your commandline run the command to create a new starter project, then
21+
navigate into the project directory:
2122

22-
```console
23-
touch main.ts
23+
```sh
24+
deno init my-express-project
25+
cd my-express-project
2426
```
2527

26-
In `main.ts`, let's create a simple server:
28+
## Install Express
29+
30+
To install Express, we'll use the `npm:` module specifier. This specifier allows
31+
us to import modules from npm:
32+
33+
```sh
34+
deno add npm:express
35+
```
36+
37+
This will add the latest `express` package to the `imports` field in your
38+
`deno.json` file. Now you can import `express` in your code with
39+
`import express from "express";`.
40+
41+
## Update `main.ts`
42+
43+
In the `main.ts`, let's create a simple server:
2744

2845
```ts
29-
// @ts-types="npm:@types/express@4.17.15"
30-
import express from "npm:express@4.18.2";
46+
import express from "express";
3147

3248
const app = express();
3349

@@ -36,15 +52,43 @@ app.get("/", (req, res) => {
3652
});
3753

3854
app.listen(8000);
55+
console.log(`Server is running on http://localhost:8000`);
3956
```
4057

41-
Let's run this server:
58+
You may notice that your editor is complaining about the `req` and `res`
59+
parameters. This is because Deno does not have types for the `express` module.
60+
To fix this, you can import the Express types file directly from npm. Add the
61+
following comment to the top of your `main.ts` file:
4262

43-
```console
44-
deno run -A main.ts
63+
```ts
64+
// @ts-types="npm:@types/express@4.17.15"
4565
```
4666

47-
And point our browser to `localhost:8000`. You should see:
67+
This comment tells Deno to use the types from the `@types/express` package.
68+
69+
## Run the server
70+
71+
When you initialized the project, Deno set up a task which will run the main.ts
72+
file, you can see it in the `deno.json` file. Update the `dev` task to include
73+
the [`--allow-net`](/runtime/fundamentals/security/#network-access) flag:
74+
75+
````jsonc
76+
{
77+
"scripts": {
78+
"dev": "deno run --allow-net main.ts"
79+
},
80+
...
81+
}
82+
83+
This will allow the project to make network requests. You can [read more about permissions flags](/runtime/fundamentals/security/).
84+
85+
Now you can run the server with:
86+
87+
```sh
88+
deno run dev
89+
````
90+
91+
If you visit `localhost:8000` in your browser, you should see:
4892
4993
**Welcome to the Dinosaur API!**
5094
@@ -53,26 +97,22 @@ And point our browser to `localhost:8000`. You should see:
5397
The next step here is to add some data. We'll use this Dinosaur data that we
5498
found from [this article](https://www.thoughtco.com/dinosaurs-a-to-z-1093748).
5599
Feel free to
56-
[copy it from here](https://github.com/denoland/examples/blob/main/with-express/data.json).
100+
[copy it from here](https://raw.githubusercontent.com/denoland/tutorial-with-express/refs/heads/main/data.json).
57101
58-
Let's create `data.json`:
102+
Create a `data.json` file in the root of your project, and paste in the dinosaur
103+
data.
59104
60-
```console
61-
touch data.json
62-
```
63-
64-
And paste in the dinosaur data.
65-
66-
Next, let's import that data into `main.ts`. Let's add this line at the top of
67-
the file:
105+
Next, we'll import that data into `main.ts`:
68106
69107
```ts
70-
import data from "./data.json" assert { type: "json" };
108+
import data from "./data.json" with { type: "json" };
71109
```
72110

73-
Then, we can create the routes to access that data. To keep it simple, let's
74-
just define `GET` handlers for `/api/` and `/api/:dinosaur`. Add the below after
75-
the `const app = express();` line:
111+
We will create the routes to access that data.
112+
113+
To keep it simple, let's just define `GET` handlers for `/api/` and
114+
`/api/:dinosaur`. Add the following code after the `const app = express();`
115+
line:
76116
77117
```ts
78118
app.get("/", (req, res) => {
@@ -97,12 +137,13 @@ app.get("/api/:dinosaur", (req, res) => {
97137
});
98138
99139
app.listen(8000);
140+
console.log(`Server is running on http://localhost:8000`);
100141
```
101142
102-
Let's run the server with `deno run -A main.ts` and check out
103-
`localhost:8000/api`. You should see a list of dinosaurs:
143+
Let's run the server with `deno run dev` and check out `localhost:8000/api` in
144+
your browser. You should see a list of dinosaurs!
104145

105-
```json
146+
```jsonc
106147
[
107148
{
108149
"name": "Aardonyx",
@@ -119,7 +160,8 @@ Let's run the server with `deno run -A main.ts` and check out
119160
...
120161
```
121162

122-
And when we go to `localhost:8000/api/aardonyx`:
163+
You can also get the details of a specific dinosaur by visiting "/api/dinosaur
164+
name", for example `localhost:8000/api/aardonyx` will display:
123165

124166
```json
125167
{
@@ -128,4 +170,6 @@ And when we go to `localhost:8000/api/aardonyx`:
128170
}
129171
```
130172

131-
Great!
173+
🦕 Now you're all set to use Express with Deno. You could consider expanding
174+
this example into a dinosaur web app. Or take a look at
175+
[Deno's built in HTTP server](https://docs.deno.com/runtime/fundamentals/http_server/).

0 commit comments

Comments
 (0)