4

我已在我的应用程序中将 html5Mode 设置为true。我的路线定义如下:

app.config(['$routeProvider', '$locationProvider',
    function ($routeProvider, $locationProvider) {
        $locationProvider.html5Mode(true);
        $locationProvider.hashPrefix('!');

        $routeProvider.
            when('/api/', {
                templateUrl: '../../html/console.html',
                controller: 'paramsController'
            }).
            when('/', {
                templateUrl: '../../html/timeline.html',
                controller: 'timelineController'
            }).
            otherwise({
                redirectTo: '/'
            });
    }]);

但是每当我发出请求时,http://localhost:3000/api它会发出 HTTP 调用并且请求会转到服务器而不是解析到我的角度路由。

我需要在这里照顾什么。

我的主页添加了以下标签:

<base href="/">

只是说明一下,以防它在这里改变任何东西。是否需要其他配置。我node.js作为服务器使用express.js.

4

2 回答 2

2

发生这种情况的原因是因为您访问的 URL 未在其上提供前端应用程序并且未正确配置服务器。

您实际上需要在服务器上重写 URL 才能正常工作。

请参阅 angularJS 官方文档上的解释。

于 2015-03-17T17:30:22.707 回答
1

正如@oLeduc 在他的评论中提到的,这是由于未在服务器端配置 URL 重写造成的。

我在服务器端路由中添加了以下内容(我使用的是 expressjs)。

app.all('/*', function(req, res) {
    console.log("Server re-writing for angular routing");
    res.sendfile('home.html');
});

这使得路由能够被重定向回为home.html.

可以从以下位置更改模式:

'/*' to '/api/*`

如果你想匹配 UI 路由说/api/test

于 2015-03-21T06:42:53.307 回答