AngularJS html5mode重新加载页面
我有两个单独的应用程序:
- AngularJS客户端工作在http:// localhost:8080
- 工作在http:// localhost:3000上的NodeJS API服务器
NodeJS API服务器只是给客户端提供json数据(并不提供任何html页面)。 因此,为了让客户端能够与服务器通信,我在API服务器上启用了CORS技术。
app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); next(); });
另外我想使用干净的URL没有#符号,所以我启用我的AngularJS应用程序中的html5mode
function config($stateProvider, $urlRouterProvider, $locationProvider) { $locationProvider.html5Mode(true); $urlRouterProvider.otherwise('/login'); ...
— index.html的
<!doctype html> <html lang="en" ng-app="app"> <head> <base href="/"> <title>APP</title> <link rel="stylesheet" href="css/normalize.css"> <link rel="stylesheet" href="css/style.css"> <link rel="stylesheet" href="css/fontello.css"> </head> <body> <div ui-view="content"></div> <script src="dist/app.js" type="text/javascript"></script> </body> </html>
问题 :当我与我的应用程序通过与ui-sref
所有工作良好的btn的,但是当我重新加载页面,我得到以下输出: Cannot GET /about
你可以build议我使用重写所有我的链接index.html例如:
app.all('/*', function(req, res, next) { res.sendFile('index.html', { root: __dirname }); });
但我没有任何“意见”在我的服务器上。
在这种情况下你能给我什么?
你猜对了!
您需要通过端口8080告诉服务器在所有请求(不包括现有的静态文件)上提供index.html文件。
然后,UI路由器会将您路由到您的angular度应用程序的相关页面客户端或redirect到另一个url,如果input的是不是一个有效的路线。