为了便于维护,我想划分我的服务器路由:
server.js(启动应用):
require('./routes/allRoutes')(app);
allRoutes.js:
module.exports = function(app){
//don't I need to pass (app) below? This will not work as is
var Group1 = require('../routes/Group1/groupRoutes')(app),
Group2 = require('../routes/Group2/groupRoutes')(app);
app.use('/group1', Group1);
app.use('/group2', Group2);
}
groupRoutes.js :(在Group1文件夹中)
module.exports = function(app){
//don't I need to pass (app) below? This will not work as is
var SubGroup1 = require('../routes/Group1/subGroup1Route')(app);
.....
app.use('/subgroup1', SubGroup1);
}
subGroup1Routes.js :(在Group1文件夹中)
var FooCrtl = require('../../controllers/Group1/subGroup1/FooCtrl');
module.exports = function(app){
app.get('/api/foo', FooCtrl.get);
app.post('/api/foo', FooCtrl.post);
}
AngularJs FooSvcs:
....
return {
getFoo: function(){
return $http({
method: 'GET',
url: '/group1/subgroup1/api/foo', <--- NOT WORKING
});
},
双重问题:
在服务器端,我不需要通过&#34; app&#34;作为路线树下的参数?我试过但是发现了一个错误。
在上面的场景中,我的角度工厂的正确网址是什么,因为我无效。
答案 0 :(得分:0)
我希望在服务器端划分我的路由结构以在客户端[角度]侧镜像我的模块结构是正确的方法。
我误以为我需要为我的服务器路由创建一个树结构 [&lt; - WRONG ],这是不案例。
客户[角度]方:
public ---- app ---- module A folder ---- module B folder ---- module C folder
服务器[express]侧面结构:
server ---- routes ---- route A folder --- foo apis --- bar apis ---- route B folder ---- route C folder
foo api:
var express = require('express'), FooCtl = require('../../controllers/foo/FooCtl'); module.exports = function(app){ app.get('/api/foo', FooCtl.get); .... }
我不需要创建&#34;父路线&#34;收集我在该文件夹中的路线。
相反,使用&#34; npm install --save wrench &#34;在我的root中,server.js:
var express = require('express'), path = require('path'), wrench = require('wrench'); wrench.readdirRecursive("routes", function (error, files) { if(files != null){ files.forEach(function (file) { if (file.indexOf('.js') > -1 && file != undefined) { fullPath = ('./routes/' + file); console.log(fullPath); require(fullPath)(app); } })} });
一切正常!