-
Notifications
You must be signed in to change notification settings - Fork 1
/
route.js
33 lines (31 loc) · 946 Bytes
/
route.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
var fs = require('fs');
/*扫描Action*/
var router = function(callback) {
var files = fs.readdirSync("./action/"),
routes = [];
for (var i = 0; i < files.length; i++) {
routes.push(routeInject(files[i]));
}
return routes;
};
/*将路由注入*/
var routeInject = function(action) {
var express = require('express'),
route = express.Router(),
actionFun = require("./action/" + action),
urlRouter = action.substr(0, action.lastIndexOf("Action.js"));
urlRouter = (urlRouter == "index" ? "" : urlRouter); /*首页是indexAction*/
function callback(req, res, next) {
if (typeof(actionFun) == "function") {
actionFun({
res: res,
req: req,
next: next
});
}
}
route.get("/" + urlRouter, callback);
route.post("/" + urlRouter, callback);
return route;
};
module.exports = router;