$ locationProvider html5mode导致“无法获取";刷新错误-AngularJS节点应用 [英] $locationProvider html5mode causing "cannot GET" error on refresh - AngularJS Node app
问题描述
我正在尝试使用locationProvider从Angularjs应用程序的URL中删除主题标签,并且在我手动刷新页面之前,它可以很好地工作.这总是在浏览器中导致无法获取.."错误.我已经进行了一些研究,我认为我必须使用.htaccess文件进行重定向,但是我不确定如何实现它,因此我在网上搜索了解决方案,但到目前为止还没有运气.
I am trying to remove the hashtags from my Angularjs app's URLs using the locationProvider, and it works well until I refresh a page manually. This always causes a "cannot GET .." error in the browser. I have done some research and I think I have to use an .htaccess file to redirect, but I am not sure how to implement it and I have searched the web for a solution but no luck so far.
这是我的app.js,它处理到我的Angular视图和控制器的路由:
Here is my app.js which handles routing to my Angular views and controllers:
(function () {
'use strict';
var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(function($routeProvider, $locationProvider) {
$routeProvider.
when('/', {
templateUrl: 'views/partials/play.html',
controller: 'playCtrl'
}).
when('/dictionary', {
templateUrl: 'views/partials/dictionary.html',
controller: 'dictionaryCtrl'
}).
when('/add', {
templateUrl: 'views/partials/word.html',
controller: 'wordCtrl'
}).
when('/about', {
templateUrl: 'views/partials/about.html',
controller: 'aboutCtrl'
}).
otherwise({redirectTo: '/'});
// use the HTML5 History API
$locationProvider.html5Mode(true);
});
})();
而且我也在我的index.html中的<head>
内添加了它:
And I have also added this inside the <head>
in my index.html:
<base href="/">
该应用是使用Node.js,Express,Angular& Mongodb,如果有帮助的话.
The app is built using Node.js, Express, Angular & Mongodb if this helps.
非常感谢.
推荐答案
如果有人想知道您是否需要将Node不使用的所有路由重定向到Angular:
In case some one else is wondering you need to redirect all routes that Node is not using to Angular:
app.get('*', function(req, res) {
res.sendfile('./path/to/index.html')
})
这必须在server.js中完成.不要忘了在sendfile链接中使用./:
This has to be done in server.js. Don't forget to use ./ in the sendfile link :)
这篇关于$ locationProvider html5mode导致“无法获取";刷新错误-AngularJS节点应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!