sails 使用者定義路由(1)

2021-08-03 16:14:29 字數 1806 閱讀 3840

sails支援兩種型別的路由: custom(or "explicit") andautomatic(or "implicit").

先來看一下custom 即使用者定義路由吧,以下是學習筆記。

在config/routes.js中定義如下類似的路由:

module.exports.routes=,

'post /signup':'authcontroller.processsignup',

'get/login': ,

'post /login':'authcontroller.processlogin',

'/logout':'authcontroller.logout',

'get /me':'usercontroller.profile'

}有的將url指向某個controller的action,有的則將url指向某個view

甚至還可以在路由中指定view使用的layout

'get /privacy':

},1.每個路由都必須包含位址和目標

'get /foo/bar':'foocontroller.bar'

^^^address^^^^^^^^^^target^^^^^^^

2.位址定義:

a.使用萬用字元和動態引數

比如:'/user/foo/*'

'/user/foo/:name/bar/:age'

'/user/foo/*/bar/*'

b.正規表示式

"r||

list of param names>"

比如:"r|^/\\d+/(\\w+)/(\\w+)$|foo,bar":"messagecontroller.myaction"

will

match/123/abc/def, running themyactionaction ofmessagecontrollerand supplying the valuesabcanddefasreq.param('foo')andreq.param('bar')

c.路由位址匹配的順序

按照routes.js中的書寫順序進行匹配,一旦匹配成功,便不會再往下繼續尋找(有高階的方法可以改變該規則,但不推薦)

3.路由目標定義

a. controller/action的語法規則:

'get /foo/go':'foocontroller.mygoaction',

'get /foo/go':'foo.mygoaction',

'get /foo/go': ,

'get /foo/go': ,

以上四種寫法等價。

需要注意的是,controller和action的名字是大小寫敏感的。

b.view目標的語法規則:

'get /team':

c. blueprint目標的語法規則

'get /findallusers': ,

'get /user/findall':

'get /user/findall':

4.定義重定向(redirect)

'/alias' :'/some/other/route'

'get /google':''

5.定義response

'/foo':

6.function定義

路由可以直接指向某個function

'/foo':function(req, res)

7.policy target syntax

路由可以為target指定policy,即在達到指定target時,必須先通過某個policy

'/foo': [, ]

sails route 1 使用者定義路由

sails支援兩種型別的路由 custom or explicit andautomatic or implicit 先來看一下custom 即使用者定義路由吧,以下是學習筆記。在config routes.js中定義如下類似的路由 module.exports.routes post signup...

Thinkphp 路由定義

thinkphp的路由 thinkphp下的 conf 下可以進行配置 154行 系統變數名稱設定 var module m 預設模組獲取變數 var addon addon 預設的外掛程式控制器命名空間變數 var controller c 預設控制器獲取變數 var action a 預設操作獲...

MVC路由 自定義路由

1 global.asax檔案註冊路由 建立mvc專案時就有 public class public class routeconfig axd 1 路由按註冊順序 優先生效 2 註冊自定義路由,場景檢查訪問瀏覽器,ip等可以進行控制。routes.add newcustomroute 3 rout...