sails.js mvc framework learning
目的:加快开发速度,总结使用方法:
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的更多相关文章
- 全端开发必备!10个最好的 Node.js MVC 框架
Node.js 是最流行的 JavaScript 服务端平台,它允许建立可扩展的 Web 应用程序.Node.js 包含不同类型的框架,如 MVC 框架.全栈框架.REST API 以及大量的服 ...
- Sails.js中文文档
Sails.js中文文档 http://sailsdoc.swift.ren/ Sails.js是一个Web框架,可以于轻松构建自定义,企业级Node.js Apps.它在设计上类似于像Ruby ...
- 十款最佳Node.js MVC框架
十款最佳Node.js MVC框架摘要:Node.js是JavaScript中最为流行的框架之一,易于创建可扩展的Web应用.本文分享十款最佳的JavaScript框架. Node.js是JavaSc ...
- 私人定制,十款最佳Node.js MVC框架
Node.js是JavaScript中最为流行的框架之一,易于创建可扩展的Web应用.本文分享十款最佳的JavaScript框架. Node.js是JavaScript中最为流行的框架之一,易于创建可 ...
- 【转】Code Your Own PHP MVC Framework in 1 Hour
原文: https://www.codeproject.com/Articles/1080626/Code-Your-Own-PHP-MVC-Framework-in-Hour --------- ...
- Sails.js中文文档,Sails中文文档
Sails.js中文文档 http://sailsdoc.swift.ren/ Sails.js是一个Web框架,可以于轻松构建自定义,企业级Node.js Apps.它在设计上类似于像Ruby ...
- Mithril – 构建杰出 Web 应用的 JS MVC 框架
Mithril 是一个客户端的 Javascript MVC 框架.它是一个工具,使应用程序代码分为数据层,UI 层和粘合层.提供了一个模板引擎与一个虚拟的 DOM diff 实现,用于高性能渲染,支 ...
- Atitit. js mvc 总结(2)----angular 跟 Knockout o99 最佳实践
Atitit. js mvc 总结(2)----angular 跟 Knockout o99 最佳实践 1. 框架 angular 跟Knockout 1 2. 2. 简单的列表绑定:Knockou ...
- 理解ASP.NET MVC Framework Action Filters
原文:http://www.cnblogs.com/darkdawn/archive/2009/03/13/1410477.html 本指南主要解释action filters,action filt ...
随机推荐
- vim必备
1. 复制 将vim中整个文件内容复制到系统剪贴板--gg"+yG 2. 格式化 缩进格式化代码--gg=G
- java笔记 -- java数据类型与类型转换
Java是一种强类型语言, 这就意味着必须为每一个变量声明一种类型. Java中一共有8中基本类型: 4种整型: 整型: 用于表示没有小数部分的数值, 允许为负数 类型 存储需求 取值范围 int: ...
- English trip EM2-PE-5A Plan a dinner party Teacher:Lamb
课上内容(Lesson) # Appetizer ['æpə'taɪzɚ] n. 开胃物,开胃食品 spinach salad 菠菜沙拉 # "p" 发b音 gazpac ...
- 高并发情况下Linux系统及kernel参数优化
众所周知在默认参数情况下Linux对高并发支持并不好,主要受限于单进程最大打开文件数限制.内核TCP参数方面和IO事件分配机制等.下面就从几方面来调整使Linux系统能够支持高并发环境. Iptabl ...
- Java IO编程全解(二)——传统的BIO编程
前面讲到:Java IO编程全解(一)——Java的I/O演进之路 网络编程的基本模型是Client/Server模型,也就是两个进程之间进行相互通信,其中服务端提供位置信息(绑定的IP地址和监听端口 ...
- linux软件管理 RPM命令
RPM命名规则 httpd -2.2.15-15.el6.centos.1.i686.rpm (包全名) httpd 软件包名 (包名) 2.2.15 软件版本 15 软件发布的次数 el6.cent ...
- 协程----greenlet模块,gevent模块
1.协程初识,greenlet模块 2.gevent模块(需要pip安装) 一.协程初识,greenlet模块: 协程:是单线程下的并发,又称微线程,纤程.英文名Coroutine.一句话说明什么是线 ...
- unity5.x中的关节和布料
关节 布料 关节 铰链关节(Hinge Joint):将两个物体以链条的形式绑在一起,当力量过大超过链条的固定力矩时,两个物体就会产生相互的拉力. 固定关节(Fixed Joint): ...
- Crontab中的除号(slash)到底怎么用?(转载)
转载于:https://www.cnblogs.com/cocowool/p/5865397.html crontab 是Linux中配置定时任务的工具,在各种配置中,我们经常会看到除号(Slash) ...
- 关于iOSlaunchScreen的尺寸
备注:这里只是个人的观点,有的地方也是copy,多多指教,个人笔记,有侵犯你们版权的地方还望海涵!!! 关于launchImage 的尺寸链接 摘自:http://www.cnblogs.com/Ri ...