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

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. Bytom信息上链教程

    比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom 用比原链 ...

  2. 【HNOI 2018】寻宝游戏

    Problem Description 某大学每年都会有一次 \(Mystery\ Hunt\) 的活动,玩家需要根据设置的线索解谜,找到宝藏的位置,前一年获胜的队伍可以获得这一年出题的机会. 作为新 ...

  3. C/C++.判断文件是否存在(_access)

    1. int _access(char* path,int mode)头文件<io.h>功能:确定文件或文件夹的访问权限.如果指定的存取方式有效,则函数返回0,否则函数返回-1. 参数pa ...

  4. oracle数据库 concat 与 ||

    1.简述: 通过几条sql语句来引入此问题 先新建一张表,插入几条数据,如下: CREATE TABLE tab1 (col1 VARCHAR2(6), col2 CHAR(6), col3 VARC ...

  5. 关于contenteditable属性

    今天刷刷看看,看到了发说说框,发现居然不是textarea的,百思不得其解围.后来看到contenteditable,心想应该就是这个搞怪的吧,百度了下.w3c的解释是contenteditable属 ...

  6. hello1

    1:选择hello1文件夹并单击“打开项目”.展开“Web页”节点,然后双击该index.xhtml文件以在编辑器中查看它. 该index.xhtml文件是Facelets应用程序的默认登录页面.在典 ...

  7. Delegate, NSNotification, KVO, Block

    delegate: 当我们第一次编写iOS应用时,我们注意到不断的在使用“delegate”,并且贯穿于整个SDK.delegation模式不是iOS特有的模式,而是依赖与你过去拥有的编程背景.针对它 ...

  8. SSH Struts2+hiberante+Spring整合

    使用SSH框架编写学生信息: 一.新建Java工程: (1)建立好Java各层级之间的结构:业务处理层dao,数据模型层domain,页面请求处理层(Struts2 MVC层)action,servi ...

  9. Java数组声明、初始化

    维数组的声明方式:type var[]; 或type[] var; 声明数组时不能指定其长度(数组中元素的个数), Java中使用关键字new创建数组对象,格式为:数组名 = new 数组元素的类型 ...

  10. format格式化和函数

    {[name][:][[fill]align][sign][#][0][width][,][.precision][type]}用{ }包裹name命名传递给format以命名=值 写法, 非字典映射 ...