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
Copy file name to clipboardexpand all lines: content/4.resources/1.learn/2.h3-101-first-hand.md
+24-29
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,6 @@ resources:
6
6
- # add a link to the documentation
7
7
---
8
8
9
-
10
9
H3 is a minimal http framework for high performance and portability.
11
10
12
11
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.
@@ -44,9 +43,9 @@ And that's it! We are ready to start coding.
44
43
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:
@@ -80,13 +79,13 @@ Now that our app is ready to accept HTTP requests, we need to create a router to
80
79
With H3, we've just to use the function `createRouter` and add it to our app:
81
80
82
81
```ts [app.ts]
83
-
import { createApp, createRouter } from'h3';
82
+
import { createApp, createRouter } from'h3'
84
83
85
-
exportconst app =createApp();
84
+
exportconst app =createApp()
86
85
87
-
const router =createRouter();
86
+
const router =createRouter()
88
87
89
-
app.use(router);
88
+
app.use(router)
90
89
```
91
90
92
91
The `app.use(router)`{lang="ts"} is necessary to add the router to our app.
@@ -105,11 +104,11 @@ To add a handler, we can use any of the HTTP methods available on the router. Fo
105
104
```ts [app.ts]
106
105
// ...
107
106
108
-
const router =createRouter();
107
+
const router =createRouter()
109
108
110
109
router.get('/', () => {
111
-
return'Hello World!';
112
-
});
110
+
return'Hello World!'
111
+
})
113
112
```
114
113
115
114
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"}.
@@ -123,51 +122,48 @@ For our app, we will return an HTML page populated with some data. This part wil
123
122
To create our fake database (a JavaScript array) with some getters and setters, we need a file named `database.ts`:
124
123
125
124
```ts [database.ts]
126
-
import { Book } from"./types";
125
+
import { Book } from'./types'
127
126
128
127
/**
129
128
* This is a fake database since it's just an array of objects.
130
129
*
131
130
* For this example, it's sufficient but do not use this in production.
0 commit comments