问题描述
我试图去除角的URL'#',我已经做了我的前端的调整像
I tried to remove '#' from angular-urls, I have done the tweaks in my front-end like
$locationProvider.html5Mode(true);
<base href="/*"> in my view file.
我使用前preSS由后端。虽然它的效果很好,但是当我刷新页面它给了我错误的URL没有找到。
I am using express in by backend. THough Its works well but when I refresh page it gives me error url not found.
我的默认URL是像前preSS渲染视图就好了,
My default url is like from express to render view is like,
//server.js
app.use(express.static('app'));
var routes = require('./routes/index');
app.use('/', routes);
/routes/index
router.all('/', function(req, res) {
res.render('home');
});
它的工作原理的情况下,如果我点击的http://本地主机:1337 /
第一。它很好地移动到这个的http://本地主机:1337 /第一参数
但是当我点击的http://本地主机:1337 /第一参数
的情况下直接去基地网址它给了我没有发现错误。
It works in case if I click http://localhost:1337/
first. It moves nicely to this http://localhost:1337/first-param
but when I click http://localhost:1337/first-param
directly without going to base url it gives me not found error.
请不要给我暗示为的.htaccess
。我不想使用的.htaccess
。
Please don't give me hint for .htaccess
. I don't want to use .htaccess
.
推荐答案
嗯..你只处理/
在您的应用程序的路线。所以,每次向服务器请求另一条路线时说/某物
,服务器不知道该怎么办。
Well.. you are only handling '/'
route in your app. So every time you ask the server for another route say '/something'
, the server does not know what to do.
试试这个(使用router.get()而不是router.all()来只处理get请求):
Try This (Use router.get() instead of router.all() to only handle get requests):
router.get('/*', function(req, res) {
res.render('home');
});
这应该处理所有的路由信息。
This should handle all the routes.
更新
当然,你可能希望通过你的前preSS应用处理等航线,说你的API。
Of course you might want to have other routes handled by your express app, say for your API.
要做到这一点,我建议你在格式创建API / API /富/ {}吧
To do that, I suggest you create your api in the format '/api/foo/{bar}'
和您将在下面的方式您的路由器:
And you set up your router in the following way :
router.route(/^((?!\/(api)).)*$/).get(function(req, res){res.render('home')});
常规前pression为不与 / API
开始的一切也是如此,并返回主页。
The regular expression holds true for everything that does not start with /api
and returns the home page.
记住,添加此配置下其它所有路由保持层次结构。
Remember, add this configuration BELOW all the other routes to maintain the heirarchy.
总结: code的最后片段处理,只是用 / API
开始的那些所有的路由,你设计API来始终与 / API
启动。使用此方法,它应该工作得很好。
To summarize : The last snippet of code handles all the routes except the ones that start with /api
and you design your API to always start with /api
. Use this method and it should work just fine.
这篇关于角URL使用前preSS到路由请求删除#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!