File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ Render JSX and [Preact] components to an HTML string.
7
7
8
8
Works in Node & the browser, making it useful for universal/isomorphic rendering.
9
9
10
+ \>\> ** [ Cute Fox-Related Demo] ( http://codepen.io/developit/pen/dYZqjE?editors=001 ) ** _ (@ CodePen)_ <<
11
+
10
12
11
13
---
12
14
@@ -59,6 +61,37 @@ console.log(html);
59
61
---
60
62
61
63
64
+ ### Render JSX / Preact / Whatever via Express!
65
+
66
+ ``` js
67
+ import express from ' express' ;
68
+ import { h } from ' preact' ;
69
+ import render from ' preact-render-to-string' ;
70
+
71
+ // silly example component:
72
+ const Fox = ({ name }) => (
73
+ < div class = " fox" >
74
+ < h5> { name }< / h5>
75
+ < p> This page is all about {name}.< / p>
76
+ < / div>
77
+ );
78
+
79
+ // basic HTTP server via express:
80
+ const app = express ();
81
+ app .listen (8080 );
82
+
83
+ // on each request, render and return a component:
84
+ app .get (' /:fox' , (req , res ) => {
85
+ let html = render (< Fox name= {req .params .fox } / > );
86
+ // send it back wrapped up as an HTML5 document:
87
+ res .send (` <!DOCTYPE html><html><body>${ html} </body></html>` );
88
+ });
89
+ ```
90
+
91
+
92
+ ---
93
+
94
+
62
95
### License
63
96
64
97
[ MIT]
You can’t perform that action at this time.
0 commit comments