Introduction to Sails.js
yungshin
About Me
• yungshin
• 大學化學系念不下去,碩班跳
坑念資工
• III IDEAS
• 上班爬網站,下班寫網站
• 喜愛鑽研前端技術
投資新技術有賺有賠,使用前請先詳閱
公開說明書(官方文件)
Sails.js
• Web MVC Framework for Node.js
• Auto Routing
• Express based
• Scaffolding
Sails.js
• Socket.io support
• Restful blueprint
• Model module, ORM, use Waterline
MVC
• M - Model
• V - View
• C - Controller
MVC
• User see the views
• User use controllers
• Controllers manipulate models
• Models update views
Get Started
Get Started
• Install Command-line tools
sudo npm install -g sails
Get Started
• Create a new app:
!
• Now lift the server:
!
• See the default home page:
sails new testProject
cd testProje...
Sails.js Command Line
• Generate Model and Controller
• sails generate [Name]
• Generate Controller
• sails generate contr...
Sails.js Command Line
• Generate Model
• sails generate model [Name] [Attribute:Type]…
• Ex: sails generate model person fi...
Controller
Controller
• http://sailsjs.org/#!documentation/controllers
• Think of controllers as being the middleman between your
mod...
Controller
Controller
• generate routes would be the following: (Action blueprints)
• /comment/create
• /comment/destroy
• /comment/t...
Controller
• REST blueprints
• You can disable it in “config/controller.js”
• find(id) -> GET /:controller/:id
• create() ->...
Request and Response
• If you need to dive deeper, check out the express guide.
Request and Response
• req.param()
• /:controller/:action/:foo
• var foo = req.param(“foo”);
• req.body.val
• form data, q...
Request and Response
• res.view([view, options[, fn]])
• res.send(body|status[, headers|status[, status]])
• res.json(obj[...
Routes
Routes
• http://sailsjs.org/#!documentation/routes
• routes urls to controllers/actions
• Path: “config/routes.js”
Routes
• static routes
!
!
!
• controller name
• controller action (function)
/post: {
controller: ‘PostController',
actio...
Routes
• route point to a view
!
!
!
• the followings point to the “view/home/index.ejs”
‘/': {
view: ‘home/index’
}
Routes
• set routes for particular http verbs
'POST /signup': {
controller: controller_name,
action: controller_action
}
Model
Model
• http://sailsjs.org/#!documentation/models
• Path: api/models
• ORM -> Waterline
• multiple adapters for databases
Model
module.exports = {
attributes: {
firstName: ‘STRING',
lastName: ‘STRING',
age: {
type: ‘INTEGER',
max: 150,
required...
Model
module.exports = {
attributes: {
firstName: ‘STRING',
lastName: ‘STRING',
age: {
type: ‘INTEGER',
max: 150,
required...
Model
module.exports = {
attributes: {
firstName: ‘STRING',
lastName: ‘STRING',
age: {
type: ‘INTEGER',
max: 150,
required...
Model
module.exports = {
attributes: {
firstName: ‘STRING',
lastName: ‘STRING',
age: {
type: ‘INTEGER',
max: 150,
required...
Model
Person.create({
firstName: ‘foo',
lastName: ‘bar',
age: 18
}).done(function(err, post) {
if(err) {
/* ... */
}
!
/* ...
View
View
• http://sailsjs.org/#!documentation/views
• render pages
• Path: views/…
• Supports:
• ejs (default) (http://embedde...
View
• render views in controller
• response.view()
return res.view("home/index", {
title: "Foo",
posts: post
});
Asset Management
Asset Management
• Path: assets/…
• css, js, images
• Grunt.js
• less, coffeescript
• concat, cssmin, uglify, …
Pros & Cons
• Pros
• Use command line to generate controllers and models
• Developing routes / Restful API is fast
• other...
Conclusion
• Sails.js is good mvc framework for Node.js
• Building routes / Restful API is fast
?
Thank you!
E-mail: yungshin.chien@gmail.com
twitter: @yungshin_chien
Introduction to Sails.js
Upcoming SlideShare
Loading in...5
×

Introduction to Sails.js

1,022

Published on

Introduction to Sails.js
Presentation for TechCCU Workshop on July 12

Published in: Engineering, Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,022
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
29
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Introduction to Sails.js

  1. 1. Introduction to Sails.js yungshin
  2. 2. About Me • yungshin • 大學化學系念不下去,碩班跳 坑念資工 • III IDEAS • 上班爬網站,下班寫網站 • 喜愛鑽研前端技術
  3. 3. 投資新技術有賺有賠,使用前請先詳閱 公開說明書(官方文件)
  4. 4. Sails.js • Web MVC Framework for Node.js • Auto Routing • Express based • Scaffolding
  5. 5. Sails.js • Socket.io support • Restful blueprint • Model module, ORM, use Waterline
  6. 6. MVC • M - Model • V - View • C - Controller
  7. 7. MVC • User see the views • User use controllers • Controllers manipulate models • Models update views
  8. 8. Get Started
  9. 9. Get Started • Install Command-line tools sudo npm install -g sails
  10. 10. Get Started • Create a new app: ! • Now lift the server: ! • See the default home page: sails new testProject cd testProject sails lift http://localhost:1337/
  11. 11. Sails.js Command Line • Generate Model and Controller • sails generate [Name] • Generate Controller • sails generate controller [Name] [Action]… • Ex: sails generate controller post create find update destroy • Path: api/controller
  12. 12. Sails.js Command Line • Generate Model • sails generate model [Name] [Attribute:Type]… • Ex: sails generate model person firstName:string lastName:string age:integer birthDate:date • Path: api/model
  13. 13. Controller
  14. 14. Controller • http://sailsjs.org/#!documentation/controllers • Think of controllers as being the middleman between your model and your views. sails generate controller comment create destroy tag like
  15. 15. Controller
  16. 16. Controller • generate routes would be the following: (Action blueprints) • /comment/create • /comment/destroy • /comment/tag • /comment/like
  17. 17. Controller • REST blueprints • You can disable it in “config/controller.js” • find(id) -> GET /:controller/:id • create() -> POST /:controller • update(id) -> PUT /:controller/:id • destroy(id) -> DELETE /:controller/:id
  18. 18. Request and Response • If you need to dive deeper, check out the express guide.
  19. 19. Request and Response • req.param() • /:controller/:action/:foo • var foo = req.param(“foo”); • req.body.val • form data, query string
  20. 20. Request and Response • res.view([view, options[, fn]]) • res.send(body|status[, headers|status[, status]]) • res.json(obj[, headers|status[, status]]) • res.redirect(url[, status])
  21. 21. Routes
  22. 22. Routes • http://sailsjs.org/#!documentation/routes • routes urls to controllers/actions • Path: “config/routes.js”
  23. 23. Routes • static routes ! ! ! • controller name • controller action (function) /post: { controller: ‘PostController', action: 'findAll' }
  24. 24. Routes • route point to a view ! ! ! • the followings point to the “view/home/index.ejs” ‘/': { view: ‘home/index’ }
  25. 25. Routes • set routes for particular http verbs 'POST /signup': { controller: controller_name, action: controller_action }
  26. 26. Model
  27. 27. Model • http://sailsjs.org/#!documentation/models • Path: api/models • ORM -> Waterline • multiple adapters for databases
  28. 28. Model module.exports = { attributes: { firstName: ‘STRING', lastName: ‘STRING', age: { type: ‘INTEGER', max: 150, required: true } } };
  29. 29. Model module.exports = { attributes: { firstName: ‘STRING', lastName: ‘STRING', age: { type: ‘INTEGER', max: 150, required: true } } }; attribute name
  30. 30. Model module.exports = { attributes: { firstName: ‘STRING', lastName: ‘STRING', age: { type: ‘INTEGER', max: 150, required: true } } }; attribute name attribute type
  31. 31. Model module.exports = { attributes: { firstName: ‘STRING', lastName: ‘STRING', age: { type: ‘INTEGER', max: 150, required: true } } }; attribute name attribute type validation
  32. 32. Model Person.create({ firstName: ‘foo', lastName: ‘bar', age: 18 }).done(function(err, post) { if(err) { /* ... */ } ! /* ... */ });
  33. 33. View
  34. 34. View • http://sailsjs.org/#!documentation/views • render pages • Path: views/… • Supports: • ejs (default) (http://embeddedjs.com/) • jade (http://jade-lang.com/) • ...
  35. 35. View • render views in controller • response.view() return res.view("home/index", { title: "Foo", posts: post });
  36. 36. Asset Management
  37. 37. Asset Management • Path: assets/… • css, js, images • Grunt.js • less, coffeescript • concat, cssmin, uglify, …
  38. 38. Pros & Cons • Pros • Use command line to generate controllers and models • Developing routes / Restful API is fast • other good things: policy… • Cons • Documentation (especially waterline) • No support for associations • Devs have not been very responsive lately
  39. 39. Conclusion • Sails.js is good mvc framework for Node.js • Building routes / Restful API is fast
  40. 40. ?
  41. 41. Thank you! E-mail: yungshin.chien@gmail.com twitter: @yungshin_chien
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×