0

server code:

app.get('/', function(req, res){
    console.log('executed "/"')
     res.render('home');
});

app.get('/partials/:name', function (req, res) {
    console.log('executed partials:name');
    var name = req.params.name;
    console.log(name);
    res.render('partials/' + name);
});

the code works perfectly before i put $locationProvider.html5Mode(true); to convert '/#/' url into regular / url.

After which, the console.log('executed partials:name'); fails to execute. Documentation says:-

Server side
Using this mode requires URL rewriting on server side, basically you have to rewrite all 
your links to entry point of your application (e.g. index.html)

The changes I have tried are not working. What are changes to be made?

Edit: The following is the angular code:

var myApp = angular.module('myApp', []);

myApp.config(['$routeProvider', function($routeProvider, $locationProvider) {
  $routeProvider.
      when('/', {
        templateUrl: 'partials/partial',
        controller: 'homePage'
      }).      
      otherwise({redirectTo: '/login'});
    $locationProvider.html5Mode(true);
}]);

I again mention, the routing works perfectly well, till i add $locationProvider.html5Mode(true); to angular.

4

2 回答 2

0

在这种情况下,错误与路由无关。myApp.config(['$routeProvider', function($routeProvider, $locationProvider)定义错误。

它应该包括$locationProvider:-

myApp.config(['$routeProvider', '$locationProvider', function($routeProvider,
 $locationProvider) {

完美运行。

于 2013-10-04T09:04:45.077 回答
0

不太确定这是否符合您的问题,但是当我们这样做时,我们需要在服务器上(在 Nginx 中使用我们的设置)创建一些规则,以确保指向/page的链接不只是返回 404,因为它不存在在服务器上。

首先,我们需要确保指向 Angular 路由的链接被这样对待。所以我们将ourdomain.com/page重写为ourdomain.com/#/page/page在服务器上不存在,仅在 Angular 中,因此它会返回 404。它需要转到索引以便 Angular 可以处理路由(这与传入链接相关,当你在从那时起,该网站无论如何都会处理链接)。

其次,我们需要对部分进行例外处理,因为它们实际上存在于服务器上,而不是 Angular 路由。所以去ourdomain.com/partials/partial其实需要在服务器上找到partial,不能被上面的规则改写。

希望这至少有所帮助。

于 2013-10-04T08:41:32.643 回答