Skip to content

Commit 442f75f

Browse files
committed
first commit
0 parents  commit 442f75f

20 files changed

+2924
-0
lines changed

.editorconfig

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
# Unix-style newlines with a newline ending every file
7+
[*]
8+
end_of_line = lf
9+
insert_final_newline = true
10+
11+
# Matches multiple files with brace expansion notation
12+
# Set default charset
13+
[*.js]
14+
charset = utf-8
15+
indent_style = space
16+
indent_size = 2
17+
18+
[{package.json,.travis.yml}]
19+
indent_style = space
20+
indent_size = 2

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
source-koa
3+
node.sh
4+
node_modules

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8.9.1

.vscode/launch.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// 使用 IntelliSense 了解相关属性。
3+
// 悬停以查看现有属性的描述。
4+
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"program": "${workspaceFolder}/app.js",
12+
"runtimeExecutable": "${env:HOME}/.nvm/versions/node/v8.9.1/bin/node"
13+
}
14+
]
15+
}

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Tiny Koa
2+
3+
Koa is already very mini framework for HTTP, but somes read the source code still hard, I wrote a lite version for Koa.
4+
With some middlewares.
5+
6+
Help you learn Koa.
7+
💪

app.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const TinyKoa = require('./src/tiny-koa.js');
2+
const app = new TinyKoa();
3+
const port = 5678;
4+
5+
const Router = require('./src/middleware/tiny-koa-router.js');
6+
const router = new Router();
7+
8+
const cors = require('./src/middleware/tiny-koa-cors.js');
9+
const bodyparser = require('./src/middleware/tiny-koa-body.js');
10+
const server = require('./src/middleware/tiny-koa-static.js');
11+
const koaCookies = require('./src/middleware/tiny-koa-cookie.js');
12+
const views = require('./src/middleware/tiny-koa-views.js');
13+
let render = views({
14+
root: __dirname + '/views'
15+
});
16+
17+
app.use(cors());
18+
app.use(koaCookies());
19+
app.use(bodyparser());
20+
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();
26+
});
27+
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();
33+
});
34+
35+
// doT测试
36+
router.all('/page/test', async (ctx, next) => {
37+
ctx.body = await render('tiny', {title: 'tiny dot template', body: 'powered by doT'});
38+
});
39+
40+
let routeMiddleware = router.routes();
41+
app.use(routeMiddleware);
42+
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 ();
49+
});
50+
51+
// app.use(async function m2 (ctx, next) {
52+
// console.log('m2');
53+
// });
54+
55+
app.use(server(__dirname + '/static'));
56+
57+
app.listen(port, () => {
58+
console.log(`listening ${port}`);
59+
});

0 commit comments

Comments
 (0)