目的:加快开发速度,总结使用方法:

menu list:

  • custom controller
  • custom 模块使用
  • custom model
  • custom middleware
  • custom
  • service

关于启动 ##:


config/connection -- db connection

入口: app.js

config/bootstrap - 启动入口app.js 后的

config/policies 配置

bin/*.json

关于DIY ##:


config/router

路由与对应的controller处理:

用命令行 & controller 变化

wade-mac:fin_server_invest mac$ sails generate controller mail sendmaillog

info: Created a new controller ("mail") at api/controllers/MailController.js!

路由总汇总:config/routes.js

想往常一样加:, 'GET /make/a/mail':"MailController.sendmaillog"

模块的使用: -- 与以前一样

 /**
* MailController
*
* @description :: Server-side logic for managing mails
* @help :: See http://links.sailsjs.org/docs/controllers
*/ module.exports = { /**
* `MailController.sendmaillog()`
*/
sendmaillog: function (req, res) {
var log4js = require('log4js');
var logger = log4js.getLogger();
logger.debug("Some debug messages"); return res.json({
todo: 'sendmaillog() is not implemented yet!'
});
}
};

model command :

https://www.digitalocean.com/community/tutorials/how-to-create-an-node-js-app-using-sails-js-on-an-ubuntu-vps

$sails generate model user name:string email:string password:string
$sails generate controller user index show edit delete

middleware:

https://gist.github.com/mikermcneil/6255295

look at config/http.js

module.exports.http = {

  /****************************************************************************
* *
* Express middleware to use for every Sails request. To add custom *
* middleware to the mix, add a function to the middleware config object and *
* add its key to the "order" array. The $custom key is reserved for *
* backwards-compatibility with Sails v0.9.x apps that use the *
* `customMiddleware` config option. *
* *
****************************************************************************/ middleware: { /***************************************************************************
* *
* The order in which middleware should be run for HTTP request. (the Sails *
* router is invoked by the "router" middleware below.) *
* *
***************************************************************************/ order: [
'startRequestTimer',
'cookieParser',
'session',
'myRequestLogger',
'bodyParser',
'handleBodyParserError',
'compress',
'methodOverride',
'poweredBy',
'$custom',
'router',
'www',
'favicon',
'404',
'500'
], /****************************************************************************
* *
* Example custom middleware; logs each request to the console. *
* *
****************************************************************************/ myRequestLogger: function (req, res, next) {
console.log("Requested :: ", req.method, req.url);
return next();
}, /***************************************************************************
* *
* The body parser that will handle incoming multipart HTTP requests. By *
* default as of v0.10, Sails uses *
* [skipper](http://github.com/balderdashy/skipper). See *
* http://www.senchalabs.org/connect/multipart.html for other options. *
* *
***************************************************************************/ bodyParser: require('skipper') }, /***************************************************************************
* *
* The number of seconds to cache flat files on disk being served by *
* Express static middleware (by default, these files are in `.tmp/public`) *
* *
* The HTTP static cache is only active in a 'production' environment, *
* since that's the only time Express will cache flat-files. *
* *
***************************************************************************/ cache: 31557600000
};

上面的是所有的路由都经过的middleware

疑问:控制某个路由/a 经过middleware: [a, b, c ] , 某个路由/b 经过middleware: [a, c ]


一些文件夹

  • service 本项目单独的业务逻辑
  • lib 存放一些共用的lib
  • data 存放常用配置、数据

CRUD

  • CRUD -- model

  • CRUD -- 尽量使用 native

    • 速度快
    • 适合复杂操作
  • CRUD -- 坑

    • 并发:使用findandmodify & update(upsert) & findorcreate

    //findandmodify

    Order.native(function (err, collection) {

    collection.findAndModify(query, null, {$set: update_data}, {'new': false}, function (err, newResult, detail) {

    //update upsert

    User_account_cashflow.native(function(err, colletion){

    colletion.update(query, {$set: cashflow },{upsert:true, multi:false} , function(err, effectNum, result){

    callback(err, cashflow, result.updatedExisting);

    });

    });


sails.js mvc framework learning的更多相关文章

  1. 全端开发必备!10个最好的 Node.js MVC 框架

      Node.js 是最流行的 JavaScript 服务端平台,它允许建立可扩展的 Web 应用程序.Node.js 包含不同类型的框架,如 MVC 框架.全栈框架.REST API  以及大量的服 ...

  2. Sails.js中文文档

    Sails.js中文文档   http://sailsdoc.swift.ren/ Sails.js是一个Web框架,可以于轻松构建自定义,企业级Node.js Apps.它在设计上类似于像Ruby ...

  3. 十款最佳Node.js MVC框架

    十款最佳Node.js MVC框架摘要:Node.js是JavaScript中最为流行的框架之一,易于创建可扩展的Web应用.本文分享十款最佳的JavaScript框架. Node.js是JavaSc ...

  4. 私人定制,十款最佳Node.js MVC框架

    Node.js是JavaScript中最为流行的框架之一,易于创建可扩展的Web应用.本文分享十款最佳的JavaScript框架. Node.js是JavaScript中最为流行的框架之一,易于创建可 ...

  5. 【转】Code Your Own PHP MVC Framework in 1 Hour

      原文: https://www.codeproject.com/Articles/1080626/Code-Your-Own-PHP-MVC-Framework-in-Hour --------- ...

  6. Sails.js中文文档,Sails中文文档

    Sails.js中文文档   http://sailsdoc.swift.ren/ Sails.js是一个Web框架,可以于轻松构建自定义,企业级Node.js Apps.它在设计上类似于像Ruby ...

  7. Mithril – 构建杰出 Web 应用的 JS MVC 框架

    Mithril 是一个客户端的 Javascript MVC 框架.它是一个工具,使应用程序代码分为数据层,UI 层和粘合层.提供了一个模板引擎与一个虚拟的 DOM diff 实现,用于高性能渲染,支 ...

  8. Atitit. js mvc 总结(2)----angular 跟 Knockout o99 最佳实践

    Atitit. js mvc 总结(2)----angular  跟 Knockout o99 最佳实践 1. 框架 angular 跟Knockout 1 2. 2. 简单的列表绑定:Knockou ...

  9. 理解ASP.NET MVC Framework Action Filters

    原文:http://www.cnblogs.com/darkdawn/archive/2009/03/13/1410477.html 本指南主要解释action filters,action filt ...

随机推荐

  1. windows(xshell)免密码登录

    windows(xshell)免密码登录 第一步生成密钥文件 打开xshell 选个工具栏的工具如下图 选择密钥类型和长度,如下图 密钥会自动生成,如下图,点击下一步即可 设置给密钥加密的密码也可以不 ...

  2. Microsoft Visual Studio已停止工作

    问题:今天在安装Visual Studio时,提示“Visual Studio installer 已停止工作” 解决办法:卸载原有的 .net  framework,在微软官网下载 .net fra ...

  3. 浅了解:react为何需要设定唯一key值(antd-table)

    一.React规范 1.1 react key的作用 当渲染重复数据的时候,React.diff会根据生成的key进行虚拟DOM渲染, 所以我们需要在遍历的地方都要加上key,例如map.for等等 ...

  4. 【shell脚本】 变量基础学习整理

    1.linux系统环境 echo 'echo /etc/profile ' >> /etc/profile echo 'echo /etc/bashrc' >> /etc/ba ...

  5. legend2---开发日志10(ajax请求的方法是否同样会执行base控制器里面的方法)

    legend2---开发日志10(ajax请求的方法是否同样会执行base控制器里面的方法) 一.总结 一句话总结:会执行的,所以写base控制器里面的方法要注意,base控制器里面的方法要以查数据为 ...

  6. Hibernate向数据库存入BLOB和CLOB类型的数据

    我选用的是byte[] +@Lob 刚开始采用的java.sql.Blob,将上传的图片getBytes()后,通过Hibernate.getLobCreator(HibernateSessionFa ...

  7. 重写console.log的一些理解

    关于重写console.log的方式通常都是这样的: console.log = (function(oriLogFunc){ return function(str) { oriLogFunc.ca ...

  8. ranch流程处理图

    ranch是开发服务端管理模板,这个模板不大,写的很经典,方便并发管理,而且性能很优秀~~ 其中比较优秀的就有cowboy~~ 看了一下ranch的源码(版本v1.2.1 下载链接https://gi ...

  9. web中静态资源和动态资源的概念及区别

    1.静态资源和动态资源的概念 简单来说: 静态资源:一般客户端发送请求到web服务器,web服务器从内存在取到相应的文件,返回给客户端,客户端解析并渲染显示出来. 动态资源:一般客户端请求的动态资源, ...

  10. java 常见语法,但是发现switch等基础,常见面试套路不会了,待补充

    1,面向对象,万物皆对象,面向对象的三个基本特性:封 装,继承,多态. 面向对象的程序都是由类组成,类封装了对象的属性和行为. 封装:隐藏对象的属性和实现细节,仅对外提供接口. 继承:实现代码复用,将 ...