@@ -13,21 +13,37 @@ simple and unopinionated with a large ecosystem of middleware.
13
13
This How To guide will show you how to create a simple API using Express and
14
14
Deno.
15
15
16
- [ View source here.] ( https://github.com/denoland/examples/tree/main/ with-express )
16
+ [ View source here.] ( https://github.com/denoland/tutorial- with-express )
17
17
18
- ## Create ` main.ts `
18
+ ## Initialize a new deno project
19
19
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:
21
22
22
- ``` console
23
- touch main.ts
23
+ ``` sh
24
+ deno init my-express-project
25
+ cd my-express-project
24
26
```
25
27
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:
27
44
28
45
``` ts
29
- // @ts-types="npm:@types/express@4.17.15"
30
- import express from " npm:express@4.18.2" ;
46
+ import express from " express" ;
31
47
32
48
const app = express ();
33
49
@@ -36,15 +52,43 @@ app.get("/", (req, res) => {
36
52
});
37
53
38
54
app .listen (8000 );
55
+ console .log (` Server is running on http://localhost:8000 ` );
39
56
```
40
57
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:
42
62
43
- ``` console
44
- deno run -A main.ts
63
+ ``` ts
64
+ // @ts-types="npm:@types/express@4.17.15"
45
65
```
46
66
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:
48
92
49
93
**Welcome to the Dinosaur API!**
50
94
@@ -53,26 +97,22 @@ And point our browser to `localhost:8000`. You should see:
53
97
The next step here is to add some data. We'll use this Dinosaur data that we
54
98
found from [this article](https://www.thoughtco.com/dinosaurs-a-to-z-1093748).
55
99
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).
57
101
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.
59
104
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 ` :
68
106
69
107
` ` ` ts
70
- import data from " ./data.json" assert { type: " json" };
108
+ import data from " ./data.json" with { type : "json " };
71
109
```
72
110
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:
76
116
77
117
```ts
78
118
app.get("/", (req, res) => {
@@ -97,12 +137,13 @@ app.get("/api/:dinosaur", (req, res) => {
97
137
});
98
138
99
139
app.listen(8000);
140
+ console.log(`Server is running on http://localhost:8000`);
100
141
```
101
142
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!
104
145
105
- ``` json
146
+ ` ` ` jsonc
106
147
[
107
148
{
108
149
"name": "Aardonyx",
@@ -119,7 +160,8 @@ Let's run the server with `deno run -A main.ts` and check out
119
160
...
120
161
` ` `
121
162
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:
123
165
124
166
` ` ` json
125
167
{
@@ -128,4 +170,6 @@ And when we go to `localhost:8000/api/aardonyx`:
128
170
}
129
171
` ` `
130
172
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