Skip to content

Commit b0be51b

Browse files
committed
update app.js
1 parent 6c2f10f commit b0be51b

File tree

8 files changed

+211
-162
lines changed

8 files changed

+211
-162
lines changed

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

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

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

5-
const router = require('koa-router')();
5+
const controller = require('./controller');
66

7-
const templating = require('./templating.js');
8-
9-
const fs = require('fs');
7+
const templating = require('./templating');
108

119
const app = new Koa();
1210

@@ -38,41 +36,8 @@ app.use(templating('view', {
3836
watch: !isProduction
3937
}));
4038

41-
// add url-route in /controllers:
42-
43-
function addMapping(router, mapping) {
44-
for (var url in mapping) {
45-
if (url.startsWith('GET ')) {
46-
var path = url.substring(4);
47-
router.get(path, mapping[url]);
48-
console.log(`register URL mapping: GET ${path}`);
49-
} else if (url.startsWith('POST ')) {
50-
var path = url.substring(5);
51-
router.post(path, mapping[url]);
52-
console.log(`register URL mapping: POST ${path}`);
53-
} else {
54-
console.log(`invalid URL: ${url}`);
55-
}
56-
}
57-
}
58-
59-
function addControllers(router) {
60-
var files = fs.readdirSync(__dirname + '/controllers');
61-
var js_files = files.filter((f) => {
62-
return f.endsWith('.js');
63-
}, files);
64-
65-
for (var f of js_files) {
66-
console.log(`process controller: ${f}...`);
67-
let mapping = require(__dirname + '/controllers/' + f);
68-
addMapping(router, mapping);
69-
}
70-
}
71-
72-
addControllers(router);
73-
74-
// add router middleware:
75-
app.use(router.routes());
39+
// add controller:
40+
app.use(controller());
7641

7742
app.listen(3000);
7843
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, dir) {
31+
var files = fs.readdirSync(__dirname + '/' + dir);
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 + '/' + dir + '/' + 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, controllers_dir);
48+
return router.routes();
49+
};

samples/node/web/rest/rest-hello/app.js

Lines changed: 3 additions & 37 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,40 +15,8 @@ app.use(async (ctx, next) => {
1715
// parse request body:
1816
app.use(bodyParser());
1917

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

5521
app.listen(3000);
5622
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, dir) {
31+
var files = fs.readdirSync(__dirname + '/' + dir);
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 + '/' + dir + '/' + 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, controllers_dir);
48+
return router.routes();
49+
};

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

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

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

5-
const router = require('koa-router')();
5+
const controller = require('./controller');
66

7-
const fs = require('fs');
8-
9-
const templating = require('./templating.js');
7+
const templating = require('./templating');
108

119
const rest = require('./rest');
1210

@@ -34,49 +32,8 @@ app.use(templating('view', {
3432
// bind .rest() for ctx:
3533
app.use(rest.restify());
3634

37-
// add url-route in /controllers:
38-
39-
function addMapping(router, mapping) {
40-
for (var url in mapping) {
41-
if (url.startsWith('GET ')) {
42-
var path = url.substring(4);
43-
router.get(path, mapping[url]);
44-
console.log(`register URL mapping: GET ${path}`);
45-
} else if (url.startsWith('POST ')) {
46-
var path = url.substring(5);
47-
router.post(path, mapping[url]);
48-
console.log(`register URL mapping: POST ${path}`);
49-
} else if (url.startsWith('PUT ')) {
50-
var path = url.substring(4);
51-
router.put(path, mapping[url]);
52-
console.log(`register URL mapping: PUT ${path}`);
53-
} else if (url.startsWith('DELETE ')) {
54-
var path = url.substring(7);
55-
router.del(path, mapping[url]);
56-
console.log(`register URL mapping: DELETE ${path}`);
57-
} else {
58-
console.log(`invalid URL: ${url}`);
59-
}
60-
}
61-
}
62-
63-
function addControllers(router) {
64-
var files = fs.readdirSync(__dirname + '/controllers');
65-
var js_files = files.filter((f) => {
66-
return f.endsWith('.js');
67-
}, files);
68-
69-
for (var f of js_files) {
70-
console.log(`process controller: ${f}...`);
71-
let mapping = require(__dirname + '/controllers/' + f);
72-
addMapping(router, mapping);
73-
}
74-
}
75-
76-
addControllers(router);
77-
78-
// add router middleware:
79-
app.use(router.routes());
35+
// add controllers:
36+
app.use(controller());
8037

8138
app.listen(3000);
8239
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, dir) {
31+
var files = fs.readdirSync(__dirname + '/' + dir);
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 + '/' + dir + '/' + 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, controllers_dir);
48+
return router.routes();
49+
};

samples/node/web/ws/ws-with-koa/app.js

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ const Koa = require('koa');
88

99
const bodyParser = require('koa-bodyparser');
1010

11-
const router = require('koa-router')();
11+
const controller = require('./controller');
1212

13-
const templating = require('./templating.js');
14-
15-
const fs = require('fs');
13+
const templating = require('./templating');
1614

1715
const WebSocketServer = ws.Server;
1816

@@ -43,41 +41,8 @@ app.use(templating('view', {
4341
watch: true
4442
}));
4543

46-
// add url-route in /controllers:
47-
48-
function addMapping(router, mapping) {
49-
for (var url in mapping) {
50-
if (url.startsWith('GET ')) {
51-
var path = url.substring(4);
52-
router.get(path, mapping[url]);
53-
console.log(`register URL mapping: GET ${path}`);
54-
} else if (url.startsWith('POST ')) {
55-
var path = url.substring(5);
56-
router.post(path, mapping[url]);
57-
console.log(`register URL mapping: POST ${path}`);
58-
} else {
59-
console.log(`invalid URL: ${url}`);
60-
}
61-
}
62-
}
63-
64-
function addControllers(router) {
65-
var files = fs.readdirSync(__dirname + '/controllers');
66-
var js_files = files.filter((f) => {
67-
return f.endsWith('.js');
68-
}, files);
69-
70-
for (var f of js_files) {
71-
console.log(`process controller: ${f}...`);
72-
let mapping = require(__dirname + '/controllers/' + f);
73-
addMapping(router, mapping);
74-
}
75-
}
76-
77-
addControllers(router);
78-
79-
// add router middleware:
80-
app.use(router.routes());
44+
// add controller middleware:
45+
app.use(controller());
8146

8247
let server = app.listen(3000);
8348

0 commit comments

Comments
 (0)