sails route(1) -用户定义路由
sails支持两种类型的路由: custom(or "explicit") andautomatic(or "implicit").
先来看一下custom 即用户定义路由吧,以下是学习笔记。
用户定义路由
在config/routes.js中定义如下类似的路由:
module.exports.routes={
'get/signup': { view: 'conversion/signup' },
'post /signup':'AuthController.processSignup',
'get/login': { view: 'portal/login' },
'post /login':'AuthController.processLogin',
'/logout':'AuthController.logout',
'get /me':'UserController.profile'
}
有的将url指向某个controller的action,有的则将url指向某个view
甚至还可以在路由中指定view使用的layout
'get /privacy': {
view:'users/privacy',
locals: {
layout:'users'
}
},
语法规则:
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': {controller:"Foo", action:"myGoAction"},
'GET /foo/go': {controller:"FooController", action:"myGoAction"},
以上四种写法等价。
需要注意的是,controller和action的名字是大小写敏感的。
b.view目标的语法规则:
'GET /team': {view:'brochure/about'}
c. Blueprint目标的语法规则
'GET /findAllUsers': {model:'user', blueprint:'find'},
'GET /user/findAll': {blueprint:'find'}
'GET /user/findAll': {blueprint:'find', model:'pet'}
4.定义重定向(redirect)
'/alias' :'/some/other/route'
'GET /google':'http://www.google.com'
5.定义response
'/foo': {response:'notFound'}
6.function定义
路由可以直接指向某个function
'/foo':function(req, res) {res.send("FOO!");}
7.Policy target syntax
路由可以为target指定policy,即在达到指定target时,必须先通过某个policy
'/foo': [{policy:'myPolicy'}, {blueprint:'find', model:'user'}]
sails route(1) -用户定义路由的更多相关文章
- Flask之基于route装饰器的路由系统(源码阅读解析)
一 路由系统 1. 在flask中配置URL和视图函数的路由时,首先需要在main.py中实例化一个app对象: from flask import Flask, render_template ap ...
- ASP.NET Web API实践系列04,通过Route等特性设置路由
ASP.NET Web API路由,简单来说,就是把客户端请求映射到对应的Action上的过程.在"ASP.NET Web API实践系列03,路由模版, 路由惯例, 路由设置"一 ...
- 07:vue定义路由
1.1 定义路由 1.说明 1. 路由是单页面应用程序(SPA)的关键,Vue提供过来路由插件,使用这个路由就要安装这个插件 2. 安装: npm install vue-router 3. 依赖于v ...
- 初学node.js-nodejs中实现用户登录路由
经过前面几次的学习,已经可以做下小功能,今天要实现的事用户登录路由. 一.users_model.js 功能:定义用户对象模型 var mongoose=require('mongoose'), S ...
- SQL SERVER中用户定义标量函数(scalar user defined function)的性能问题
用户定义函数(UDF)分类 SQL SERVER中的用户定义函数(User Defined Functions 简称UDF)分为标量函数(Scalar-Valued Function)和表值函数(T ...
- 应用C#和SQLCLR编写SQL Server用户定义函数
摘要: 文档阐述使用C#和SQLCLR为SQL Server编写用户定义函数,并演示用户定义函数在T-SQL中的应用.文档中实现的 Base64 编码解码函数和正则表达式函数属于标量值函数,字符串分割 ...
- SQL——用户定义函数
根据用户定义函数返回值的类型,可将用户定义函数分为如下三个类别: (1) 返回值为可更新表的函数 若用户定义函数包含单个 SELECT 语句且该语句可更新,则该函数返回的表也可更新,这样的函数称为内嵌 ...
- JMeter学习-014-JMeter 配置元件实例之 - 用户定义的变量 参数化配置
前文讲述了通过 CSV Data Set Config 实现参数化配置(详情敬请参阅:JMeter学习-010-JMeter 配置元件实例之 - CSV Data Set Config 参数化配置), ...
- Sql server 浅谈用户定义表类型
1.1 简介 SQL Server 中,用户定义表类型是指用户所定义的表示表结构定义的类型.您可以使用用户定义表类型为存储过程或函数声明表值参数,或者声明您要在批处理中或在存储过程或函数的主体中使用的 ...
随机推荐
- Goroutine并发调度模型深度解析之手撸一个协程池
golanggoroutine协程池Groutine Pool高并发 并发(并行),一直以来都是一个编程语言里的核心主题之一,也是被开发者关注最多的话题:Go语言作为一个出道以来就自带 『高并发』光环 ...
- 重新=》easyui DataGrid是否可以动态的改变列显示的顺序
$.extend($.fn.datagrid.methods,{ columnMoving: function(jq){ return jq.each(function(){ var target = ...
- 编译内核出现"mkimage" command not found - U-Boot images will not be built
参考链接: http://spyker729.blogspot.com/2010/07/mkimage-command-not-found-u-boot-images.html 制作uImage的工具 ...
- C# 创建XML文件
private void CreateXMLFile(string pathAndFileName) { XmlDocument doc = new XmlDocument(); XmlElement ...
- Angular2 兼容 UC浏览器、QQ浏览器、猎豹浏览器
找到/src/polyfills.ts文件 把/** IE9, IE10 and IE11 requires all of the following polyfills. **/下注释掉的代码恢复 ...
- Android测试:从零开始3—— Instrumented单元测试1
Instrumented单元测试是指运行在物理机器或者模拟机上的测试,这样可以使用Android framework 的API和supporting API.这会在你需要使用设备信息,如app的Con ...
- Session过期后自动跳转到登录页面的实例代码
1.在项目的web.xml文件中添加如下代码: ? 1 2 3 4 <!--添加Session监听器--> <listener> <listener-class> ...
- vue模糊查询
模糊查询匹配结果 <!-- 搜索框 --> <div class="search-wrapper"> <input type="text&q ...
- Java 字符串转成运算公式
GroovyShell 实现 public static void main(String args[]) { Binding binding = new Binding(); binding.set ...
- linux 配置svn服务器+使用+注意事项
本文以ubuntu系统进行安装. 1.安装svn服务器 apt-get install subversion 输入 y 回车确认安装. 安装完毕后可以用 下边的命令查看是否安装完成,如果现实出版本号和 ...