Node.js 角度和帆路由配置
是否有任何Sails.js(或节点)配置可以阻止角度路由工作 不管我采取什么方法,除了赛尔斯路线中的路线,每一条路线都返回404 我已经尝试了1.2和1.3的Angular版本,我使用的是Sails V0.9.15 所有脚本均按正确顺序加载(例如): 我还使用位置提供程序:Node.js 角度和帆路由配置,node.js,angularjs,sails.js,Node.js,Angularjs,Sails.js,是否有任何Sails.js(或节点)配置可以阻止角度路由工作 不管我采取什么方法,除了赛尔斯路线中的路线,每一条路线都返回404 我已经尝试了1.2和1.3的Angular版本,我使用的是Sails V0.9.15 所有脚本均按正确顺序加载(例如): 我还使用位置提供程序: myApp.config(function ($locationProvider) { $locationProvider.html5Mode(true); }); 其他详情: ng应用程序和ng视图位置正确,我的
myApp.config(function ($locationProvider) {
$locationProvider.html5Mode(true);
});
其他详情:
ng应用程序和ng视图位置正确,我的路径正确。我可以用Angular正确地显示profile.html(还可以包含来自Sails的RESTfulAPI的数据)
问题是,我只能对Sails'routes.js中定义的路线执行此操作
例如:
module.exports.routes = {
'/' : {
controller: 'main',
action: 'index'
},
'/signup' : {
controller: 'main',
action: 'signup'
},
'/login' : {
controller: 'main',
action: 'login'
},
'/profile': {
controller: 'users',
action: 'profile'
} //......
所以基本上,为了用Angular显示一些html内容,我必须在Sails的配置中定义完全相同的路线,这是没有意义的
有什么想法吗?另外,如果需要,我会提供额外的数据。
尝试删除html5模式,看看会发生什么:
$locationProvider.html5Mode(false);
如果您使用sails应用程序仅为Angular应用程序提供API,但使用相同的后端来提供Angular代码,那么您可以在所有API路由前加上“API”,以防止与Angular路由发生冲突
您将使用/api/profile而不是/profile
编辑: 我研究了Sails.js框架,并制作了一个小应用程序来测试它 我成功地在角度工作中获得了帆未定义的路线。 我认为对角度布线的工作原理存在误解 如果使用window.location更改路径或手动键入url,浏览器将向服务器发送get请求。因此,在您的情况下,将有一个/profile或/profilee请求,服务器将查看可用的路由,如果没有匹配的路由,将抛出404 为了避免这种情况,您应该实际使用角度方法更改路径。Angular在路径中使用“#”符号,以防止浏览器在url更改时向服务器发送请求。浏览器忽略“#”符号后的更改。或者,在您的案例中,使用html5模式可以实现类似的效果。不过要注意,当用户刷新页面时,使用html5模式可能会导致问题,因为这时会向服务器发出请求(下面将详细介绍如何修复该问题) 因此,您应该使用javascript更改路径的是服务。在角度视图中,您还可以使用普通的锚定标记,如,因为角度分析:
<a href="/profilee">Go to profile</a>
关于sail中的API,您可以在config/controllers.js文件中配置前缀:
/**
* `prefix`
*
* An optional mount path for all blueprint routes on a controller, including `rest`,
* `actions`, and `shortcuts`. This allows you to continue to use blueprints, even if you
* need to namespace your API methods.
*
* For example, `prefix: '/api/v2'` would make the following REST blueprint routes
* for a FooController:
*
* `GET /api/v2/foo/:id?`
* `POST /api/v2/foo`
* `PUT /api/v2/foo/:id`
* `DELETE /api/v2/foo/:id`
*
* By default, no prefix is used.
*/
prefix: '',
我已经尝试过不使用HTML5模式(但为了进行理智检查,再次尝试,谢谢),但没有任何运气。例如,如果我将window.location更改为'profilee'并相应地更新Angular route,我将再次得到404,如下图所示:@gazdac您可以查看使用浏览器的开发工具发出的网络请求,并准确地告诉我哪个url将返回404吗?@gazdac我扩展了我的答案,请查看。哇,谢谢现在已经很晚了,但我会试试你的解决方案,然后给你一个回复!刷新页面时如何修复此问题?当我刷新页面时,我仍然得到404
<a href="/profilee">Go to profile</a>
// What about the ever-popular "vanity URLs" aka URL slugs?
// (you might remember doing this with `mod_rewrite` in Apache)
//
// This is where you want to set up root-relative dynamic routes like:
// http://yourwebsite.com/twinkletoez
//
// NOTE:
// You'll still want to allow requests through to the static assets,
// so we need to set up this route to ignore URLs that have a trailing ".":
// (e.g. your javascript, CSS, and image files)
'get /*(^.*)': 'UserController.profile'
/**
* `prefix`
*
* An optional mount path for all blueprint routes on a controller, including `rest`,
* `actions`, and `shortcuts`. This allows you to continue to use blueprints, even if you
* need to namespace your API methods.
*
* For example, `prefix: '/api/v2'` would make the following REST blueprint routes
* for a FooController:
*
* `GET /api/v2/foo/:id?`
* `POST /api/v2/foo`
* `PUT /api/v2/foo/:id`
* `DELETE /api/v2/foo/:id`
*
* By default, no prefix is used.
*/
prefix: '',