- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontroller.js
More file actions
Latest commit
46 lines (41 loc) · 1.48 KB
/
Copy pathcontroller.js
File metadata and controls
46 lines (41 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
constfs=require('fs');
// add url-route in /controllers:
functionaddMapping(router,mapping){
for(varurlinmapping){
if(url.startsWith('GET ')){
varpath=url.substring(4);
router.get(path,mapping[url]);
console.log(`register URL mapping: GET ${path}`);
}elseif(url.startsWith('POST ')){
varpath=url.substring(5);
router.post(path,mapping[url]);
console.log(`register URL mapping: POST ${path}`);
}elseif(url.startsWith('PUT ')){
varpath=url.substring(4);
router.put(path,mapping[url]);
console.log(`register URL mapping: PUT ${path}`);
}elseif(url.startsWith('DELETE ')){
varpath=url.substring(7);
router.del(path,mapping[url]);
console.log(`register URL mapping: DELETE ${path}`);
}else{
console.log(`invalid URL: ${url}`);
}
}
}
functionaddControllers(router,dir){
fs.readdirSync(__dirname+'/'+dir).filter((f)=>{
returnf.endsWith('.js');
}).forEach((f)=>{
console.log(`process controller: ${f}...`);
letmapping=require(__dirname+'/'+dir+'/'+f);
addMapping(router,mapping);
});
}
module.exports=function(dir){
let
controllers_dir=dir||'controllers',
router=require('koa-router')();
addControllers(router,controllers_dir);
returnrouter.routes();
};