【发布时间】:2015-05-22 06:02:17
【问题描述】:
我在服务器上使用 NodeJS + expressJS,在客户端使用 AngularJS
AngularJS 控制器有以下路由:
app.config(function($routeProvider,$locationProvider) {
$routeProvider
.when('/field1/:id', {
controller: 'ctrl1',
templateUrl: "/view/mainpages/field1.html"
})
.when('/field2/:id', {
controller: 'ctrl2',
templateUrl: "/view/mainpages/field2.html"
})
........... many many
.when('/', {
controller: 'ctrl0',
templateUrl: "/view/mainpages/index.html"
})
.otherwise({redirectTo : '/'});
NodeJS 静态:
app.all('/*', express.static(__dirname + '/client'));
在调用直接通过此链接的情况下,如何使服务器重定向其路径上相对于根目录的所有静态页面。
示例:
server:3000/field1/23 必须致电server:3000/client/index.html
并且索引必须像server:3000/js/cntr1.js一样上传JS
更新:我找到了解决问题的方法。但它看起来像拐杖一样丑
app.get('/field1/([0-9]+$)', function(req, res, next) {
req.url='/index.html';
next();
});
app.use('/field1', express.static(__dirname + '/client'));
也许有更优雅的解决方案?
【问题讨论】:
标签: javascript angularjs node.js express angularjs-routing