简单的页面应用程序路由到相同的视图或控制器 SailsJS [英] Simple page app routes to same view or controller SailsJS
问题描述
如何将多个 url 路由到同一个控制器或视图以使用 angular 单页应用程序?!
How can I route multiple urls to the same controller or view to work with angular single page app?!
我可以这样做,但我认为它很丑..
I can do this but i think is ugly..
'/': {
view: 'homepage'
},
'/login': {
view: 'homepage'
},
'/register': {
view: 'homepage'
},
'/troller': {
view: 'homepage'
},
............
我想要一些类似的东西
['/','/login','/register','/troller'] -> view: 'homepage'
还有一个问题,我可以使用正则表达式进行路由吗?
And other question, Can I use regular expressions for routing?
谢谢!!对不起我的英语.
Thx!! and sorry for my english.
推荐答案
您目前无法使用真正的正则表达式进行路由.但是,您可以使用通配符路由来执行您想要的操作(在一个视图中瞄准多个路由).
You can't currently use real regular expressions for routing. You can however use a wildcard route to do what you want (aim multiple routes at one view).
把它放在你的 /config/routes.js
对象的末尾:
Put this at the end of your /config/routes.js
object:
'/*': function(req, res, next) {
if (req.path.match(/\..*/g)) {
return next();
} else {
return res.view('homepage');
}
}
和上面的路由之一不匹配的所有内容都将执行该功能.该函数首先检查您是否正在尝试访问静态资产(具有文件扩展名的内容,例如 .js
或 .css
),如果是,则继续匹配路由,以便 Express 静态中间件可以为文件提供服务.否则,它将为您的视图提供服务.
and everything that isn't matched by one of the routes above it will execute that function. The function first checks if you're trying to access a static asset (something with a file extension, like .js
or .css
), and if so, continues matching the route so that the Express static middleware can server the file. Otherwise, it will server your view.
更新
从 Sails v0.10.0-rc5 开始,可以使用正则表达式来定义路由.来自文档:
As of Sails v0.10.0-rc5, regular expressions can be used to define routes. From the docs:
正则表达式路由的语法是:
The syntax for a regular expression route is:
"r|<regular expression string>|<comma-delimited list of param names>"
这是字母r",后跟一个管道、一个不带分隔符的正则表达式字符串、另一个管道和一个参数名称列表,这些参数名称应该映射到正则表达式中带括号的组.例如:
That's the letter "r", followed by a pipe, a regular expression string without delimiters, another pipe, and a list of parameter names that should be mapped to parenthesized groups in the regular expression. For example:
"r|^/\d+/(\w+)/(\w+)$|foo,bar": "MessageController.myaction"
将匹配/123/abc/def,运行MessageController
的myaction
动作并提供值abc
和 def
分别为 req.param('foo')
和 req.param('bar')
.
will match /123/abc/def, running the myaction
action of MessageController
and supplying the values abc
and def
as req.param('foo')
and req.param('bar')
, respectively.
这篇关于简单的页面应用程序路由到相同的视图或控制器 SailsJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!