1
1
const TinyKoa = require ( './src/tiny-koa.js' ) ;
2
2
const app = new TinyKoa ( ) ;
3
- const port = 5678 ;
3
+ const port = 8600 ;
4
4
5
5
const Router = require ( './src/middleware/tiny-koa-router.js' ) ;
6
6
const router = new Router ( ) ;
@@ -18,40 +18,50 @@ app.use(cors());
18
18
app . use ( koaCookies ( ) ) ;
19
19
app . use ( bodyparser ( ) ) ;
20
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 ( ) ;
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
26
37
} ) ;
27
38
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
33
46
} ) ;
34
47
35
48
// doT测试
36
- router . all ( '/page/test ' , async ( ctx , next ) => {
49
+ router . all ( '/page/tpl ' , async ( ctx , next ) => {
37
50
ctx . body = await render ( 'tiny' , { title : 'tiny dot template' , body : 'powered by doT' } ) ;
38
51
} ) ;
39
52
40
53
let routeMiddleware = router . routes ( ) ;
41
54
app . use ( routeMiddleware ) ;
42
55
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
+ }
49
63
} ) ;
50
64
51
- // app.use(async function m2 (ctx, next) {
52
- // console.log('m2');
53
- // });
54
-
55
65
app . use ( server ( __dirname + '/static' ) ) ;
56
66
57
67
app . listen ( port , ( ) => {
0 commit comments