当html5Mode为true时,AngularJs路由向服务器发起HTTP调用
我已经在我的应用程序中将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调用,并请求去服务器,而不是解决我的angular度路线。
这是什么我需要在这里照顾。
我有以下标签添加到我的主页:
<base href="/">
只是说,万一它在这里改变任何东西。 是否需要其他configuration? 我正在使用node.js
作为express.js
的服务器。
发生这种情况的原因是因为您正在访问的前端应用程序未被提供服务的URL和服务器configuration不正确。
实际上,您需要在服务器上重写URL才能正常工作。
请参阅angularJS官方文档的解释。
正如@oLeduc在他的评论中提到的,这是由于在服务器端没有configurationURL重写造成的。
我在服务器端路由中添加了以下内容(我正在使用expressjs)。
app.all('/*', function(req, res) { console.log("Server re-writing for angular routing"); res.sendfile('home.html'); });
这使得路由被redirect回到为home.html
configuration的angular路由。
可以从以下方式改变模式:
'/*' to '/api/*`
如果你想匹配UI路由说/api/test