UNPKG

1.14 kBMarkdownView Raw
1# Koa + Marko
2
3See the [marko-koa](https://github.com/marko-js-samples/marko-koa) sample
4project 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
14require("marko/node-require");
15
16const Koa = require("koa");
17const app = new Koa();
18
19const template = require("./index.marko");
20
21app.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
30app.listen(8080);
31```
32
33You may also easily add `gzip` streaming support without additional dependencies:
34
35```javascript
36require("marko/node-require");
37const { createGzip } = require("zlib");
38
39const Koa = require("koa");
40const app = new Koa();
41
42const template = require("./index.marko");
43
44app.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
59app.listen(8080);
60```