-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamicRouter.js
32 lines (31 loc) · 928 Bytes
/
dynamicRouter.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
var express = require("express");
var router = express.Router();
var appContext
var url = require("url");
//dynamicRouter
function dynamicRouter(app){
appContext = app;
router.use(manageAction);
appContext.use(router);
}
function manageAction(req,res,next){
var path; //le path aprés le port 3000 dans l'url
var type; //GET ou POST
var controler; //nom du controleur à charger
path = url.parse(req.url).pathname;
//il faut supprimer pour le routage le param aprés l'action
if (path.split('/').length>0) path='/'+path.split('/')[1]
//console.log('Path url: ',path.split('/')[1]);
type =req.method;
console.log('path: ',path);
if (typeof GLOBAL.actions_json[type+path]=='undefined'){
console.log("Erreur pas d'action: "+path);
next();
}
else{
instanceModule = require('./routes/'+GLOBAL.actions_json[type+path].controler);
router.use(path,instanceModule);
next();
}
}
module.exports = dynamicRouter;