1 | # Koa + Marko
|
2 |
|
3 | See the [marko-koa](https://github.com/marko-js-samples/marko-koa) sample
|
4 | project for a fully-working example.
|
5 |
|
6 | ## Installation
|
7 |
|
8 | npm install koa --save
|
9 | npm install marko --save
|
10 |
|
11 | ## Usage
|
12 |
|
13 | ```javascript
|
14 | require("marko/node-require");
|
15 |
|
16 | const Koa = require("koa");
|
17 | const app = new Koa();
|
18 |
|
19 | const template = require("./index.marko");
|
20 |
|
21 | app.use((ctx, next) => {
|
22 | ctx.type = "html";
|
23 | ctx.body = template.stream({
|
24 | name: "Frank",
|
25 | count: 30,
|
26 | colors: ["red", "green", "blue"]
|
27 | });
|
28 | });
|
29 |
|
30 | app.listen(8080);
|
31 | ```
|
32 |
|
33 | You may also easily add `gzip` streaming support without additional dependencies:
|
34 |
|
35 | ```javascript
|
36 | require("marko/node-require");
|
37 | const { createGzip } = require("zlib");
|
38 |
|
39 | const Koa = require("koa");
|
40 | const app = new Koa();
|
41 |
|
42 | const template = require("./index.marko");
|
43 |
|
44 | app.use((ctx, next) => {
|
45 | ctx.type = "html";
|
46 | ctx.body = template.stream({
|
47 | name: "Frank",
|
48 | count: 30,
|
49 | colors: ["red", "green", "blue"]
|
50 | });
|
51 |
|
52 | ctx.vary("Accept-Encoding");
|
53 | if (ctx.acceptsEncodings("gzip")) {
|
54 | ctx.set("Content-Encoding", "gzip");
|
55 | ctx.body = ctx.body.pipe(createGzip());
|
56 | }
|
57 | });
|
58 |
|
59 | app.listen(8080);
|
60 | ```
|