Skip to content

Commit 386b37a

Browse files
committed
refactor url2-koa
1 parent 5351a24 commit 386b37a

File tree

2 files changed

+52
-38
lines changed

2 files changed

+52
-38
lines changed

samples/node/web/koa/url2-koa/app.js

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ const Koa = require('koa');
22

33
const bodyParser = require('koa-bodyparser');
44

5-
const router = require('koa-router')();
6-
7-
const fs = require('fs');
5+
const controller = require('./controller');
86

97
const app = new Koa();
108

@@ -17,41 +15,8 @@ app.use(async (ctx, next) => {
1715
// parse request body:
1816
app.use(bodyParser());
1917

20-
// add url-route in /controllers:
21-
22-
function addMapping(router, mapping) {
23-
for (var url in mapping) {
24-
if (url.startsWith('GET ')) {
25-
var path = url.substring(4);
26-
router.get(path, mapping[url]);
27-
console.log(`register URL mapping: GET ${path}`);
28-
} else if (url.startsWith('POST ')) {
29-
var path = url.substring(5);
30-
router.post(path, mapping[url]);
31-
console.log(`register URL mapping: POST ${path}`);
32-
} else {
33-
console.log(`invalid URL: ${url}`);
34-
}
35-
}
36-
}
37-
38-
function addControllers(router) {
39-
var files = fs.readdirSync(__dirname + '/controllers');
40-
var js_files = files.filter((f) => {
41-
return f.endsWith('.js');
42-
}, files);
43-
44-
for (var f of js_files) {
45-
console.log(`process controller: ${f}...`);
46-
let mapping = require(__dirname + '/controllers/' + f);
47-
addMapping(router, mapping);
48-
}
49-
}
50-
51-
addControllers(router);
52-
53-
// add router middleware:
54-
app.use(router.routes());
18+
// add controllers:
19+
app.use(controller());
5520

5621
app.listen(3000);
5722
console.log('app started at port 3000...');
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
const fs = require('fs');
3+
4+
// add url-route in /controllers:
5+
6+
function addMapping(router, mapping) {
7+
for (var url in mapping) {
8+
if (url.startsWith('GET ')) {
9+
var path = url.substring(4);
10+
router.get(path, mapping[url]);
11+
console.log(`register URL mapping: GET ${path}`);
12+
} else if (url.startsWith('POST ')) {
13+
var path = url.substring(5);
14+
router.post(path, mapping[url]);
15+
console.log(`register URL mapping: POST ${path}`);
16+
} else if (url.startsWith('PUT ')) {
17+
var path = url.substring(4);
18+
router.put(path, mapping[url]);
19+
console.log(`register URL mapping: PUT ${path}`);
20+
} else if (url.startsWith('DELETE ')) {
21+
var path = url.substring(7);
22+
router.del(path, mapping[url]);
23+
console.log(`register URL mapping: DELETE ${path}`);
24+
} else {
25+
console.log(`invalid URL: ${url}`);
26+
}
27+
}
28+
}
29+
30+
function addControllers(router) {
31+
var files = fs.readdirSync(__dirname + '/controllers');
32+
var js_files = files.filter((f) => {
33+
return f.endsWith('.js');
34+
}, files);
35+
36+
for (var f of js_files) {
37+
console.log(`process controller: ${f}...`);
38+
let mapping = require(__dirname + '/controllers/' + f);
39+
addMapping(router, mapping);
40+
}
41+
}
42+
43+
module.exports = function (dir) {
44+
let
45+
controllers_dir = dir || 'controllers',
46+
router = require('koa-router')();
47+
addControllers(router);
48+
return router.routes();
49+
};

0 commit comments

Comments
 (0)