简体   繁体   English

Angular HTTP服务器页面刷新404

[英]Angular http-server page refresh 404s

I have an angular app with a controller routed like so: 我有一个带有控制器路由的角度应用程序,如下所示:

controllers/favorites.js 控制器/favorites.js

...
$stateProvider.state('app.favorites' {url: '/favorites'} ...);

index.html index.html

...
<a ui-sref="app.favorites"> ...

When you click the <a> tag, it takes you to the proper controller, routes the page to /favorites and everything is dandy. 当您单击<a>标记时,它将带您到正确的控制器,将页面路由到/favorites ,一切都变得很繁琐。 However, when you refresh the page, I get a 404 (the url is still /favorites ) 但是,当您刷新页面时,我得到了404(URL仍然是/favorites

Any idea why this is? 知道为什么会这样吗? I'm running the app using http-server , but I also get the same issue with python -mSimpleHTTPServer 我正在使用http-server运行应用程序,但是python -mSimpleHTTPServer也遇到了相同的问题

It's possible to change the page location without reloading the page via the DOM's 'history' API. 可以通过DOM的“历史记录” API更改页面位置而无需重新加载页面。 Your backend doesn't automatically know or care about the way a frontend app does this. 您的后端不会自动知道或关心前端应用程序执行此操作的方式。

After your Angular app has navigated the browser to the location /favorites , when you refresh you will hit your backend at the location /favorites . 在Angular应用程序将浏览器导航到/favorites位置之后,刷新时,您将在/favorites位置找到后端。

Does your backend react to a GET request at /favorites ? 您的后端对/favorites的GET请求有反应吗? Perhaps it should! 也许应该! It's generally considered 'best practice' to allow users to refresh without 404ing. 通常认为“最佳做法”是允许用户刷新而不使用404。

This is perhaps a good time to break DRY (Don't Repeat Yourself); 这也许是打破DRY(不要重复自己)的好时机; both the frontend and the backend need knowledge of your app's routes. 前端和后端都需要了解您的应用程序的路由。

It probably has something to do with how angular loads the views. 它可能与角度如何加载视图有关。 When you hit the index page it loads all the views at the same time. 当您点击索引页面时,它会同时加载所有视图。 I think the problem is since you're refreshing on the same page it just doesn't know to reload the views. 我认为问题在于,由于您在同一页面上刷新,只是不知道重新加载视图。

I would look into running using grunt serve for a dev server as it auto refreshes every time you edit a file and it can be quite handy. 我会考虑使用grunt serve为开发服务器运行,因为它每次您编辑文件时都会自动刷新,因此非常方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM