-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclay-router.js
47 lines (42 loc) · 1.21 KB
/
clay-router.js
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
47
/* eslint-disable no-dupe-class-members */
'use strict'
var express = require('express')
class ClayRouter {
constructor(options){
this.controllerPath = options.controllerPath,
this.middlewarePath = options.middlewarePath,
this.router = express.Router()
}
get group(){
return this.group
}
get getHandlerController(){
return this.getHandlerController
}
get getMiddleware(){
return this.getMiddleware
}
getHandlerController(handler){
// eslint-disable-next-line no-undef
let controleFile = `${process.env.PWD}/${this.controllerPath}/${handler.split('@')[0]}.js`
let method = handler.split('@')[1]
return require(controleFile)[method]
}
getMiddleware(handler){
// eslint-disable-next-line no-undef
let middlewareFile = `${process.env.PWD}/${this.middlewarePath}/${handler.split('@')[0]}.js`
let method = handler.split('@')[1]
return require(middlewareFile)[method]
}
group(path, middlewares, handlers){
var middlewaresMethods= middlewares.map(m=>{return this.getMiddleware(m)})
handlers.map(h=>{
try {
this.router[h.method](path+h.path,...middlewaresMethods,this.getHandlerController(h.handler))
} catch (error) {
console.log(error)
}
})
}
}
module.exports = ClayRouter