刷新页面上的回送角度404 [英] loopback angular 404 on refresh page
问题描述
我创建了一个具有环回和角度的应用程序,但是我遇到了问题.当我刷新浏览器回送时,请给我一个未找到的404网址.
I have create an application with loopback and angular but i have a problem. When i refresh the browser loopback give me a 404 url not Found.
将基本标记添加到index.html
Added base tag to index.html
<base href="/"/>
正确设置middlelware.json以提供静态内容:
Set middlelware.json properly to serve static content:
"files": {
"loopback#static": {
"params": "$!../client"
}
在角度路由器中设置HTML5模式(我正在使用ui-router)
Set HTML5 mode in angular router (I'm using ui-router)
$locationProvider.html5Mode(true);
$urlRouterProvider.otherwise('/');
我尚未从项目中删除root.js.
I have yet deleted root.js from the project.
我做错了什么?预先感谢
What i'm doing wrong? Thanks in advance
推荐答案
在angularjs中启用html5模式/推送状态模式为ON时,这是服务器应处理的常见情况.如果我正确理解了您的问题,则服务器在刷新页面时会返回404,否则,如果从登录页面进行导航,该页面将呈现良好的效果.让我知道是否是这种情况:
When you have enabled html5 mode/ push state mode ON in angularjs, this is a common scenario which your server should handle. If I understood your problem correctly, server returns 404 when you refresh the page which otherwise render fine if navigated from landing page. Let me know if this is the scenario:
- Angular应用程序进入主屏幕,例如 your_domain/
- 导航到其他页面-您的域/某页(如果使用哈希爆炸模式,则为您的域/#somepage )
- 刷新页面->抛出404
- Angular app lands into Home screen, say your_domain/
- Navigate to some other page - your_domain/somepage (which will be your_domain/#somepage in case of hash bang mode)
- Refresh the page -> throws 404
如果您面对的情况如上所示,那么会发生这种情况:
If the scenario which you face is like given above, then this is what happens:
- 加载主页->引导角度并设置路由
- 导航到"somepage"->有角度的路线会处理并显示"somepage"
- 刷新页面->请求进入服务器并请求您的域/某页-服务器中不可用
- 服务器返回404
- Loads the home page -> angular is bootstraped and routing is set up
- Navigates to "somepage" -> angular route handles this and show "somepage"
- Refresh the page -> request hits the server and requests for your_domain/somepage - which is not available in server
- server returns 404
如何处理? 如果发生404,是否从服务器将Url重写回 your_domain/.这将引导角度应用程序,角度路由将处理请求
How to handle this? Do Url rewrite back to your_domain/ from server in case of 404. This will bootstrap the angular app and angular route will handle the request
More details here - https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode
从上述网站复制粘贴
Apache重写
<VirtualHost *:80>
ServerName my-app
DocumentRoot /path/to/app
<Directory /path/to/app>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
</VirtualHost>
Nginx重写
server {
server_name my-app;
root /path/to/app;
location / {
try_files $uri $uri/ /index.html;
}
}
Azure IIS重写
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
快速重写
var express = require('express');
var app = express();
app.use('/js', express.static(__dirname + '/js'));
app.use('/dist', express.static(__dirname + '/../dist'));
app.use('/css', express.static(__dirname + '/css'));
app.use('/partials', express.static(__dirname + '/partials'));
app.all('/*', function(req, res, next) {
// Just send the index.html for other files to support HTML5Mode
res.sendFile('index.html', { root: __dirname });
});
app.listen(3006); //the port you want to use
ASP.Net C#重写
在Global.asax中
In Global.asax
private const string ROOT_DOCUMENT = "/default.aspx";
protected void Application_BeginRequest( Object sender, EventArgs e )
{
string url = Request.Url.LocalPath;
if ( !System.IO.File.Exists( Context.Server.MapPath( url ) ) )
Context.RewritePath( ROOT_DOCUMENT );
}
这篇关于刷新页面上的回送角度404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!