15
15
---
16
16
17
17
# HTML Plugin
18
- This plugin adds shortcut for returning HTML
18
+ Return [ ** JSX ** ] ( #jsx ) , and HTML with proper HTML headers automatically
19
19
20
20
Install with:
21
21
``` bash
@@ -27,8 +27,7 @@ Then use it:
27
27
import { Elysia } from ' elysia'
28
28
import { html } from ' @elysiajs/html'
29
29
30
- const page = ` <!DOCTYPE HTML>
31
- <html lang="en">
30
+ const page = ` <html lang="en">
32
31
<head>
33
32
<title>Hello World</title>
34
33
</head>
@@ -40,11 +39,52 @@ const page = `<!DOCTYPE HTML>
40
39
new Elysia ()
41
40
.use (html ())
42
41
.get (' /' , () => page )
43
- .get (' /html' , ({ html }) => html (page ))
44
42
.listen (8080 )
45
43
```
46
44
47
- This plugin detects if the string is started with ` <!DOCTYPE HTML> ` , it will add ` Content-Type: text/html; charset=utf8 ` to the response header
45
+ If any html tag is returned, response will be treat as HTML response automatically
46
+
47
+ ## JSX
48
+ Starting from HTML plugin > 0.6.1, you can directly use JSX to create, and return HTML automatically.
49
+
50
+ ### Setup
51
+ To utilize JSX, modify the tsconfig.json as the following:
52
+ ``` json
53
+ {
54
+ "compilerOptions" : {
55
+ "jsx" : " react" ,
56
+ "jsxImportSource" : " ElysiaJSX"
57
+ }
58
+ }
59
+ ```
60
+
61
+ and that's it! 🎉
62
+
63
+ You can now use JSX to define your web page and Elysia will turns them to HTML automatically.
64
+
65
+ ``` tsx
66
+ // app.tsx
67
+ import { Elysia } from ' elysia'
68
+ import { html } from ' @elysiajs/html'
69
+
70
+ new Elysia ()
71
+ .use (html ())
72
+ .get (' /' , () => (
73
+ <html lang = " en" >
74
+ <head >
75
+ <title >Hello World</title >
76
+ </head >
77
+ <body >
78
+ <h1 >Hello World</h1 >
79
+ </body >
80
+ </html >
81
+ ))
82
+ .listen (8080 )
83
+ ```
84
+
85
+ ::: tip
86
+ To use JSX, don't forget to rename your file extension to either ` .tsx ` or ` .jsx `
87
+ :::
48
88
49
89
## Handler
50
90
Below are the value added to the handler.
56
96
``` typescript
57
97
html (value : string ) => Response
58
98
```
99
+
100
+ #### Example:
101
+ Although we recommended relying on automatic response, but you can optionally you can return explictly return any string with HTML header.
102
+
103
+ ``` typescript
104
+ import { Elysia } from ' elysia'
105
+ import { html } from ' @elysiajs/html'
106
+
107
+ const page = ` <html lang="en">
108
+ <head>
109
+ <title>Hello World</title>
110
+ </head>
111
+ <body>
112
+ <h1>Hello World</h1>
113
+ </body>
114
+ </html> `
115
+
116
+ new Elysia ()
117
+ .use (html ())
118
+ .get (' /' , ({ html }) => html (page ))
119
+ .listen (8080 )
120
+ ```
0 commit comments