Skip to content

Commit 5e377e6

Browse files
committed
add demo
1 parent 442f75f commit 5e377e6

File tree

5 files changed

+78
-28
lines changed

5 files changed

+78
-28
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,14 @@ With some middlewares.
55

66
Help you learn Koa.
77
💪
8+
9+
## Run
10+
11+
Use Node.js 8.9.1+
12+
13+
```
14+
$ npm install
15+
$ npm run dev
16+
```
17+
18+
open `http://localhost:8600/index.html`

app.js

+32-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const TinyKoa = require('./src/tiny-koa.js');
22
const app = new TinyKoa();
3-
const port = 5678;
3+
const port = 8600;
44

55
const Router = require('./src/middleware/tiny-koa-router.js');
66
const router = new Router();
@@ -18,40 +18,50 @@ app.use(cors());
1818
app.use(koaCookies());
1919
app.use(bodyparser());
2020

21-
// curl http://localhost:5678/api/abc/12
22-
router.all('/api/abc/:id', (ctx, next) => {
23-
console.log('/api/abc/:id');
24-
ctx.body = '/api/abc/:id';
25-
return next();
21+
app.use(async function ck (ctx, next) {
22+
console.log('set cookie', ctx.path);
23+
ctx.cookies.set('path', decodeURIComponent(ctx.path));
24+
await next ();
25+
});
26+
27+
// curl http://localhost:8600/api/foo/123
28+
router.all('/api/foo/:id', (ctx, next) => {
29+
// console.log('/api/foo/:id');
30+
ctx.body = {
31+
param: {
32+
id: ctx.params.id
33+
},
34+
route: '/api/foo/:id'
35+
};
36+
// return next(); // if match not to next
2637
});
2738

28-
// curl http://localhost:5678/api/ccc
29-
router.all('/api/ccc', (ctx, next) => {
30-
console.log('/api/ccc');
31-
ctx.body = '/api/ccc';
32-
return next();
39+
// curl http://localhost:8600/api/ccc
40+
router.all('/api/bar', (ctx, next) => {
41+
// console.log('/api/bar');
42+
ctx.body = {
43+
route: '/api/bar'
44+
};
45+
// return next(); // if match not to next
3346
});
3447

3548
// doT测试
36-
router.all('/page/test', async (ctx, next) => {
49+
router.all('/page/tpl', async (ctx, next) => {
3750
ctx.body = await render('tiny', {title: 'tiny dot template', body: 'powered by doT'});
3851
});
3952

4053
let routeMiddleware = router.routes();
4154
app.use(routeMiddleware);
4255

43-
app.use(async function m1 (ctx, next) {
44-
console.log('m1', ctx.path, ctx.method);
45-
ctx.body = 'm1';
46-
console.log(ctx.cookies.get(), 'ck');
47-
ctx.cookies.set('a', 'b');
48-
await next ();
56+
app.use(async (ctx, next) => {
57+
console.log('body test', ctx.req.body);
58+
if (ctx.path === '/api/fetch') {
59+
ctx.body = ctx.req.body;
60+
} else {
61+
return next();
62+
}
4963
});
5064

51-
// app.use(async function m2 (ctx, next) {
52-
// console.log('m2');
53-
// });
54-
5565
app.use(server(__dirname + '/static'));
5666

5767
app.listen(port, () => {

static/index.html

+29-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
<!DOCTYPE html>
2-
<html lang="en">
2+
<html>
33
<head>
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7-
<title>tiny koa</title>
7+
<title>Tiny Koa</title>
88
<link rel="stylesheet" href="./style.css">
9+
<script>
10+
const port = 8600;
11+
const hostname = 'http://localhost';
12+
const host = hostname + ':' + port;
13+
</script>
914
</head>
1015
<body>
11-
16+
<h1>Tiny Koa</h1>
17+
<button id="fetchData">Fetch Data</button>
18+
<br>
19+
<a href="javascript:fetch(`${host}/api/foo/123`)">http://localhost:8600/api/foo/123</a>
20+
<a href="javascript:fetch(`${host}/api/bar`)">http://localhost:8600/api/bar</a>
21+
<a href="http://localhost:8600/page/tpl" target="_blank">http://localhost:8600/page/tpl</a>
1222
</body>
13-
<script src="./script.js"></script>
23+
<script src="./script.js"></script>
24+
<script>
25+
fetchData.onclick = () => {
26+
fetch(`${host}/api/fetch`, {
27+
method: 'post',
28+
mode: 'cors',
29+
headers: {
30+
'Content-Type': 'application/json'
31+
},
32+
body: JSON.stringify({
33+
desc: 'Tiny',
34+
for: 'Koa',
35+
})
36+
});
37+
}
38+
</script>
1439
</html>

static/script.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
console.log('tiny koa');
1+
console.log('Tiny Koa');

static/style.css

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
body {
2-
background: lightcoral;
2+
background: #fefefe;
3+
}
4+
h1 {
5+
color: lightsalmon;
6+
text-align: center;
37
}

0 commit comments

Comments
 (0)