-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tmpl.txt
752 lines (537 loc) · 19.3 KB
/
tmpl.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
# Fastro
Fast and simple web application framework for deno.
With [near-native perfomance](https://deno.land/x/fastro@{{version}}/benchmarks),
you can:
- Manage your routing, middlewares, and dependencies cleanly.
- Simplifies JSON, HTML, JSX, and SSR initiation.
- Take advantage of existing Deno objects and methods.
### Getting started
Create a `main.ts` file for deno-cli entry point.
```ts
import application from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
app.get("/", () => "Hello world");
console.log("Listening on: http://localhost:8000");
await app.serve();
```
Run the app
```
deno run -A main.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/hello_world.ts)
## More examples
- [Custom Port](#custom-port)
- [JSON Response](#json-response)
- [JSON with Native Response](#json-with-native-response)
- [JSON with Fastro Response](#json-with-fastro-response)
- [HTML with Native Response](#html-with-native-response)
- [HTML with Fastro Response](#html-with-fastro-response)
- [HTML with React JSX](#html-with-react-jsx)
- [HTML Render with Eta Template Engine](#html-render-with-eta-template-engine)
- [HTML Render with SSR](#html-render-with-ssr)
- [Cookie with Native Response](#cookie-with-native-response)
- [Cookie with Fastro Response](#cookie-with-fastro-response)
- [Send custom HTTP Status, Content Type, and Authorization](#send-custom-http-status-content-type-and-authorization)
- [Routing](#routing)
- [Route Parameters](#route-parameters)
- [Router Middleware](#router-middleware)
- [Router Middleware with Array](#route-level-middleware-with-array)
- [Application Level Middleware](#application-level-middleware)
- [Application Level Middleware with Array](#application-level-middleware-with-array)
- [Route Level Middleware](#route-level-middleware)
- [Route Level Middleware with Array](#route-level-middleware-with-array)
- [SQLite and Dependency Injection](#sqlite-and-dependency-injection)
### Custom port
```ts
import application from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
app.get("/", () => new Response("Hello world!"));
await app.serve({ port: 3000 });
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/custom_port.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/custom_port.ts)
### JSON Response
```ts
import application from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
const json = { text: "Hello world" };
app.get("/", () => json);
console.log("Listening on: http://localhost:8000");
await app.serve();
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/json_response_default.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/json_response_default.ts)
### JSON with Native Response
```ts
import application from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
app.get("/", () => {
const json = { text: "Hello world" };
return new Response(JSON.stringify(json), {
status: 200,
headers: {
"content-type": "application/json",
},
});
});
console.log("Listening on: http://localhost:8000");
await app.serve();
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/json_response_native.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/json_response_native.ts)
### JSON with Fastro Response
```ts
import application, { response } from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
app.get("/", () => {
return response().json({ text: "Hello world" });
});
console.log("Listening on: http://localhost:8000");
await app.serve();
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/json_response_fastro.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/json_response_fastro.ts)
### HTML with Native Response
```ts
import application from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
app.get("/", () => {
return new Response("<html> Hello world </html>", {
status: 200,
headers: {
"content-type": "text/html",
},
});
});
console.log("Listening on: http://localhost:8000");
await app.serve();
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/html_response_native.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/html_response_native.ts)
### HTML with Fastro Response
```ts
import application, { response } from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
app.get("/", () => response().html("<h2>Hello world</h2"));
console.log("Listening on: http://localhost:8000");
await app.serve();
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/html_response_fastro.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/html_response_fastro.ts)
### HTML with React JSX
```tsx
import application from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
app.get("/", () => <h1>Hello world</h1>);
console.log("Listening on: http://localhost:8000");
await app.serve();
```
#### tsconfig: `deno.json`
```json
{
"compilerOptions": {
"strict": true,
"jsx": "react-jsx",
"jsxImportSource": "https://esm.sh/react"
}
}
```
```
deno run -A --config deno.json https://deno.land/x/fastro@{{version}}/examples/html_response_jsx.tsx
```
### Send Custom HTTP Status, Content Type, and Authorization
```ts
import application, { response } from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
app.get("/", () => {
return response()
.contentType("application/json")
.authorization("Basic YWxhZGRpbjpvcGVuc2VzYW1l")
.status(200)
.send(JSON.stringify({ message: "Hello world" }));
});
console.log("Listening on: http://localhost:8000");
await app.serve();
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/response_status.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/response_status.ts)
### Cookie with Native Response
```ts
import {
Cookie,
deleteCookie,
getCookies,
setCookie,
} from "https://deno.land/std@0.133.0/http/cookie.ts"
import application from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
app.get("/set", () => {
const headers = new Headers();
const cookie: Cookie = { name: "Space", value: "Cat" };
setCookie(headers, cookie);
return new Response(JSON.stringify(cookie), {
headers,
});
});
app.get("/delete", () => {
const headers = new Headers();
deleteCookie(headers, "Space");
const cookies = getCookies(headers);
return new Response(JSON.stringify(cookies), {
headers,
});
});
app.get("/check", (req: Request) => {
const cookie = getCookies(req.headers);
return new Response(JSON.stringify(cookie));
});
console.log("Listening on: http://localhost:8000");
await app.serve();
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/cookies_native.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/cookies_native.ts)
### Cookie with Fastro Response
```ts
import application, {
Cookie,
getCookies,
response,
} from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
app.get("/set", () => {
const cookie: Cookie = { name: "Space", value: "Cat" };
return response()
.setCookie(cookie)
.send(JSON.stringify(cookie));
});
app.get("/delete", () => {
return response()
.deleteCookie("Space")
.send("Cookie deleted");
});
app.get("/check", (req: Request) => {
const cookie = getCookies(req.headers);
return response().send(JSON.stringify(cookie));
});
console.log("Listening on: http://localhost:8000");
await app.serve();
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/cookies_fastro.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/cookies_fastro.ts)
### HTML Render with Eta Template Engine
```ts
import application from "https://deno.land/x/fastro@{{version}}/server/mod.ts";import { render } from "https://deno.land/x/eta@1.12.3/mod.ts";
const app = application();
const headers = new Headers();
headers.set("Content-Type", "text/html; charset=UTF-8");
app.get("/", () => {
const html = <string> render(
"<h4>The answer to everything is <%= it.answer %></h4>",
{
answer: 42,
},
);
return new Response(html, { headers });
});
console.log("Listening on: http://localhost:8000");
app.serve();
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/html_render.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/html_render.ts)
### Routing
```ts
import application from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
app.get("/abcd", () => new Response("/abcd"));
app.get("/ef?gh", () => new Response("/ef?gh"));
app.get("/ij+kl", () => new Response("/ij+kl"));
app.get("/mn*op", () => new Response("mn*op"));
app.get("/qr(st)?u", () => new Response("qr(st)?u"));
app.get(/v/, () => new Response("/v/"));
app.get(/.*fast$/, () => new Response("/.*fast$/"));
await app.serve();
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/routing.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/routing.ts)
### Route parameters
```ts
import application, {
getParam,
getParams,
} from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
app.get("/:id/user/:name", (req: Request) => {
const params = getParams(req);
return new Response(JSON.stringify({ params }));
});
app.get("/post/:id", (req: Request) => {
const param = getParam("id", req);
return new Response(param);
});
await app.serve();
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/route_params.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/route_params.ts)
### Router Middleware
```ts
import application, {
ConnInfo,
Next,
router,
} from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
const r = router();
const middleware = (_req: Request, _connInfo: ConnInfo, next: Next) => {
console.log("v2 - 1");
next();
};
r.get("/", () => new Response("Get"))
.post("/", () => new Response("Post"))
.put("/", () => new Response("Put"))
.delete("/", () => new Response("Delete"));
app.use("/v1", r);
app.use("/v2", middleware, r);
await app.serve();
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/router_middleware.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/router_middleware.ts)
### Router Middleware with Array
```ts
import application, {
ConnInfo,
Next,
router,
} from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
const r = router();
const middlewares = [(_req: Request, _connInfo: ConnInfo, next: Next) => {
console.log("v2 - 1");
next();
}, (_req: Request, _connInfo: ConnInfo, next: Next) => {
console.log("v2 - 2");
next();
}];
r.get("/", () => new Response("Get"))
.post("/", () => new Response("Post"))
.put("/", () => new Response("Put"))
.delete("/", () => new Response("Delete"));
app.use("/v1", r);
app.use("/v2", middlewares, r);
await app.serve();
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/router_middleware_with_array.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/router_middleware_with_array.ts)
### Application Level Middleware
```ts
import application, {
ConnInfo,
Next,
} from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
app.use((_req: Request, _conn: ConnInfo, next: Next) => {
console.log("app middleware #1");
next();
});
app.use((_req: Request, _conn: ConnInfo, next: Next) => {
console.log("app middleware #2");
next();
});
app.use((_req: Request, _conn: ConnInfo, next: Next) => {
console.log("app middleware #3");
next();
}, (_req: Request, _conn: ConnInfo, next: Next) => {
console.log("app middleware #4");
next();
});
app.get("/", () => new Response("App level #1"));
await app.serve();
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/application_level_middleware.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/application_level_middleware.ts)
### Application Level Middleware with Array
```ts
import application, {
ConnInfo,
Next,
} from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
const middlewares = [(_req: Request, _conn: ConnInfo, next: Next) => {
console.log("middleware #1");
next();
}, (_req: Request, _conn: ConnInfo, next: Next) => {
console.log("middleware #2");
next();
}, (_req: Request, _conn: ConnInfo, next: Next) => {
console.log("middleware #3");
next();
}, (_req: Request, _conn: ConnInfo, next: Next) => {
console.log("middleware #4");
next();
}];
app.use(middlewares);
app.get("/", () => new Response("App level #1"));
await app.serve();
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/application_level_middleware_with_array.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/application_level_middleware_with_array.ts)
### Route Level Middleware
```ts
import application, {
ConnInfo,
Next,
} from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
const middlewares = (_req: Request, _conn: ConnInfo, next: Next) => {
console.log("middleware #1");
next();
};
app.get("/", middlewares, () => new Response("App level #1"));
await app.serve();
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/route_level_middleware.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/route_level_middleware.ts)
### Route Level Middleware with Array
```ts
import application, {
ConnInfo,
Next,
} from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
const middlewares = [(_req: Request, _conn: ConnInfo, next: Next) => {
console.log("middleware #1");
next();
}, (_req: Request, _conn: ConnInfo, next: Next) => {
console.log("middleware #2");
next();
}, (_req: Request, _conn: ConnInfo, next: Next) => {
console.log("middleware #3");
next();
}, (_req: Request, _conn: ConnInfo, next: Next) => {
console.log("middleware #4");
next();
}];
app.get("/mnop", middlewares, () => new Response("Route level middleware #3"));
await app.serve();
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/route_level_middleware_with_array.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/route_level_middleware_with_array.ts)
### SQLite and Dependency Injection
```ts
import application, { dependency } from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
const app = application();
const db = new DB("test.db");
const deps = dependency();
deps.set("hello", () => "Hello world");
deps.set("db", db);
app.use(deps);
app.get("/", () => {
type FunctionType = () => string;
const fn = <FunctionType> app.getDeps("hello");
return new Response(fn());
});
app.post("/name", () => {
const db = <DB> app.getDeps("db");
db.query(`CREATE TABLE IF NOT EXISTS people (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT)`);
const names = ["Peter Parker", "Clark Kent", "Bruce Wayne"];
for (const name of names) {
db.query("INSERT INTO people (name) VALUES (?)", [name]);
}
return new Response(JSON.stringify(names));
});
app.get("/name", () => {
const db = <DB> app.getDeps("db");
const res = db.query("SELECT name FROM people");
return new Response(JSON.stringify(res));
});
console.log("Listening on: http://localhost:8000");
await app.serve();
```
```
deno run -A https://deno.land/x/fastro@{{version}}/examples/deps_injection.ts
```
[![alt text](https://raw.githubusercontent.com/fastrodev/fastro/gh-pages/assets/img/deno-deploy-button.svg)](https://dash.deno.com/new?url=https://deno.land/x/fastro@{{version}}/examples/deps_injection.ts)
### HTML Render with SSR
#### Component
Create react component: `app.tsx`
```tsx
import React from "https://esm.sh/react@17.0.2";
const App = () => {
const [count, setCount] = React.useState(0);
return (
<div>
<h1>Hello Deno Land!</h1>
<button onClick={() => setCount(count + 1)}>Click the 🦕</button>
<p>You clicked the 🦕 {count} times</p>
</div>
);
};
export default App;
```
#### Endpoint
Create routing file: `api.tsx`
```tsx
import application, { response } from "https://deno.land/x/fastro@{{version}}/server/mod.ts";
import rendering from "https://deno.land/x/fastro@{{version}}/server/ssr.ts";
import App from "./app.tsx";
const ssr = rendering().component(<App />);
const app = application(ssr)
.static("/static")
.get("/", () => {
return response(ssr).render({ title: "Hello world" });
});
console.log("Listening on: http://localhost:8000");
await app.serve();
```
#### Configuration
tsconfig: `deno.json`
```json
{
"compilerOptions": {
"strict": true,
"jsx": "react-jsx",
"jsxImportSource": "https://esm.sh/react"
}
}
```
#### How to run locally
```
deno run -A --unstable api.tsx
```