简体   繁体   English

您如何在Node + Ember应用程序中处理路由?

[英]How do you handle routing in a Node + Ember application?

I have a Node + Ember application. 我有一个Node + Ember应用程序。 I ran into an issue around routes. 我遇到了围绕路线的问题。 Both Node and Ember handle routes and I have some routes that I would like Node to handle but some, I want Ember to handle. Node和Ember都可以处理路由,我有一些我希望Node处理的路由,但是有些我希望Ember处理。 For instance, when the page loads, Node's router kicks in so any route I declare in Ember is useless. 例如,当页面加载时,Node的路由器启动,因此我在Ember中声明的任何路由都是无用的。

Assume that I have a route called 'settings' in Ember JS. 假设我在Ember JS中有一条称为“设置”的路由。 When I navigate to ' http://myapp.com/settings ' by using the {{#link-to}} helper from another view in Ember, I can see the page. 当我从Ember的另一个视图使用{{#link-to}}帮助程序导航到“ http://myapp.com/settings ”时,可以看到此页面。 However, once I am on that page and hit reload, I get a 404 because Node's router is called and that route is not declared in Node. 但是,一旦我进入该页面并点击重新加载,我就会得到一个404,因为调用了Node的路由器,并且该路由未在Node中声明。 How do I resolve this? 我该如何解决?

I don't use node but I imagine the concept is probably the same for you as it is for my app (I'm using Java Spring). 我不使用节点,但我想这个概念对您和我的应用程序可能是相同的(我正在使用Java Spring)。 You should modify your route to app.get('/*') or however you use wildcards in node and return Ember's index.html. 您应该修改到app.get('/*')路由,或者在节点中使用通配符并返回Ember的index.html。 So basically, any url under / aka all of them get your Ember app, not just the / route 因此,基本上,/之下的所有网址都可以获取Ember应用程序,而不仅仅是/ route

Ember handles the rest. 其余部分由灰烬处理。 Take a look at this thread (its for nginx, but again the principal is the same): http://discuss.emberjs.com/t/how-to-serve-all-routes-on-a-production-server-exactly/6372/2 看一下该线程(它用于nginx,但原理再次相同): http : //discuss.emberjs.com/t/how-to-serve-all-routes-on-a-production-server-恰好/二分之六千三百七十二

In case that link goes down at some point in the future, here's the useful nginx configuration that rewrites all rules under / except for assets with the ember index.html page: 万一将来某个时候该链接断开,以下是有用的nginx配置,该配置重写/下的所有规则,但带有ember index.html页面的assets除外:

server {
    listen 80 default;
    server_name my.domain.com;
    root /path/to/app/root;

    location / {
        rewrite ^ /index.html break;
    }

    location /assets/ {
        # do nothing and let nginx handle this as usual
    }
}

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

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