You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: Get started with H3 by building a simple app.
4
+
authors:
5
+
- name: Estéban S
6
+
picture: https://github.com/barbapapazes.png
7
+
twitter: soubiran_
8
+
category: getting-started
9
+
packages:
10
+
- h3
4
11
resources:
5
-
- # add a link to the examples repo
6
-
- # add a link to the documentation
12
+
- label: Source Code
13
+
to: https://github.com/unjs/examples/tree/main/h3/build-your-first-app
14
+
icon: i-simple-icons-github
15
+
- label: H3 Documentation
16
+
to: https://h3.unjs.io
17
+
icon: i-heroicons-book-open
18
+
- label: H3 Examples
19
+
to: https://github.com/unjs/h3/tree/main/examples
20
+
icon: i-simple-icons-github
21
+
publishedAt: 2024-02-11
22
+
modifiedAt: 2024-02-11
7
23
---
8
24
9
-
10
25
H3 is a minimal http framework for high performance and portability.
11
26
12
-
During this tutorial, we will create a simple app to get a wide overview of H3 capabilities. This app will serve an HTML file populated with data. There will be some forms to add and remove data. At the end, we will see how to add an API endpoint to get the data in JSON format.
27
+
During this tutorial, we will create a very simple app to get a wide overview of H3 capabilities. This app will serve an HTML file populated with data. There will be some forms to add and remove data. At the end, we will see how to add an API endpoint to get the data in JSON format.
13
28
14
29
> [!NOTE]
15
30
> Deep dive into H3 through [the dedicated documentation](https://h3.unjs.io).
16
31
32
+
> [!TIP]
33
+
> For more complexe apps, take a look at [Nitro](https://nitro.unjs.io).
34
+
17
35
## Prerequisites
18
36
19
37
To follow this tutorial, we need to have [Node.js](https://nodejs.org/en/) installed on our machine with [npm](https://www.npmjs.com/). We also need to have a basic knowledge of JavaScript.
20
38
21
39
> [!NOTE]
22
-
> Despite H3 is written in TypeScript, you don't need to know TypeScript to use it.
40
+
> Despite H3 is written in TypeScript, we don't need to know TypeScript to use it.
23
41
24
42
## Create a New Project
25
43
@@ -41,12 +59,12 @@ And that's it! We are ready to start coding.
41
59
42
60
## Create the App
43
61
44
-
To create our first H3 app, we need to create an `app.ts` file at the root of our project. Inside, we will create a new app by importing the `createApp` function from H3 and calling it:
62
+
To create our first H3 app, we need to create an `app.mjs` file at the root of our project. Inside, we will create a new app by importing the `createApp` function from H3 and calling it:
@@ -102,114 +120,51 @@ We have an app and a router. The only thing missing is the handlers. A handler i
102
120
103
121
To add a handler, we can use any of the HTTP methods available on the router. For our tutorial, we will use the `get` method to handle the `GET` requests.
104
122
105
-
```ts [app.ts]
123
+
```js [app.mjs]
106
124
// ...
107
125
108
-
const router =createRouter();
126
+
constrouter=createRouter()
109
127
110
128
router.get('/', () => {
111
-
return'Hello World!';
112
-
});
113
-
```
114
-
115
-
In the code above, we added a handler for the `/` route. This handler will send the string `Hello World!` to the client with a simple `return`{lang="ts"}.
For our app, we will return an HTML page populated with some data. This part will not be explained in details since it's not the purpose of this tutorial.
122
-
123
-
To create our fake database (a JavaScript array) with some getters and setters, we need a file named `database.ts`:
124
-
125
-
```ts [database.ts]
126
-
import { Book } from"./types";
127
-
128
-
/**
129
-
* This is a fake database since it's just an array of objects.
130
-
*
131
-
* For this example, it's sufficient but do not use this in production.
In the code above, we added a handler for the `/` route. This handler will send the string `Hello World!` to the client with a simple `return`{lang="js"}.
182
136
183
-
> [!IMPORTANT]
184
-
> This is a fake database since it's just an array of objects. For this example, it's sufficient but **do not use this in production**.
For this first route, we will get the books from the database and render them in an HTML page. For each book, we will add a for to remove it from the database. Under the list, we will add a form to add a new book.
141
+
For this first route, we will get the books from a static array and render them in an HTML page. For each book, we will add a for to remove it from the database. Under the list, we will add a form to add a new book.
189
142
190
143
For the style, we will use [Pico CSS](https://picocss.com/).
In our HTML page, we have two forms. One to add a book and one to remove a book. We need to add two new routes to handle them.
204
+
In our HTML page, we have two forms. One to add a book and one to remove a book. We need to add two new routes to handle them. This is interesting because we will need to handle the body of the request.
250
205
251
206
### Add a Book
252
207
@@ -257,22 +212,26 @@ npm install zod
257
212
```
258
213
259
214
> [!NOTE]
260
-
> Zod is a TypeScript-first schema declaration and validation library. It's not mandatory to use it with H3 but it's a good practice to validate the data since it's runtime agnostic.
215
+
> Zod is a schema validation with TypeScript type inference. It's not mandatory to use it with H3 but it's a recommended practice to validate the user data.
@@ -283,27 +242,38 @@ There is two important things to notice in this code.
283
242
284
243
First, we use the `readValidatedBody` function to read the body of the request and validate it. It's important to validate the data sent by the client to avoid any security issue.
285
244
245
+
> [!NOTE]
246
+
> We can use the `readBody` function to read the body of the request without validation.
247
+
286
248
Second, we use the `sendRedirect` function to redirect the client to the previous page. We use the `referer` header to get the previous page. If the header is not present, we redirect to the root page.
287
249
288
250
> [!NOTE]
289
251
> We should think to redirect to `/` but using the referer is a better practice if we change the root page.
@@ -351,4 +322,4 @@ And voilà! We now have our first H3 app!
351
322
352
323
During this course, we saw how to create a H3 app, use a listener with it, create a router, add handlers, validate data. But there is a lot more to discover about H3 on [the dedicated documentation](https://h3.unjs.io).
353
324
354
-
Then, do not hesitate to take a look at [Nitro](https://nitro.unjs.io) to create more advanced web servers that run everywhere.
325
+
However, H3 is very low level and has very specific use cases. In general, we recommend [Nitro](https://nitro.unjs.io) to create more advanced web servers with database that run everywhere.
0 commit comments