Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building RESTful APIs in Sails.js

Aaron Shea
November 07, 2013

Building RESTful APIs in Sails.js

A short lightning talk on Sails.js for http://newhaven.io meetup

Aaron Shea

November 07, 2013
Tweet

Other Decks in Programming

Transcript

  1. ABOUT ME Aaron M. Shea 17 Year old developer Intern

    at DigitalSurgeons Find me on stuff! https://github.com/aaron524 LinkedIn Facebook Twitter
  2. EXPRESS Web application platform for Node Open-ended Small Library (~930

    LOC) No dependency on any database, ORM, template engine etc.
  3. EXPRESS Downside(s) There is no defined structure for an Express

    project Routing? MVC? Where do I put my controllers? My Models?
  4. SAILS.JS (Real-time) MVC Framework Built on top of Express Provides

    a template of the "correct" way to do MVC Robust ORM Support for existing Express libraries and template engines
  5. CRUD blueprints are generated by default! RESOURCEFUL ROUTING! G E

    T / : c o n t r o l l e r = > f i n d A l l ( ) G E T / : c o n t r o l l e r / r e a d / : i d = > f i n d ( i d ) P O S T / : c o n t r o l l e r / c r e a t e = > c r e a t e ( ) P O S T / : c o n t r o l l e r / c r e a t e / : i d = > c r e a t e ( i d ) P U T / : c o n t r o l l e r / u p d a t e / : i d = > u p d a t e ( i d ) D E L E T E / : c o n t r o l l e r / d e s t r o y / : i d = > d e s t r o y ( i d ) s a i l s g U s e r P O S T / a p i / u s e r / { n a m e : " J o h n " , p e t : " C a t " } P O S T / a p i / u s e r / { n a m e : " M i k e " , p e t : " S e a g u l l " } P O S T / a p i / u s e r / { n a m e : " L y r a " , p e t : " P o n y " } G E T / a p i / u s e r /
  6. GET /API/USER { " n a m e " :

    " J o h n " , " p e t " : " C a t " , " c r e a t e d A t " : " 2 0 1 3 - 1 1 - 0 6 T 0 4 : 0 7 : 1 9 . 2 7 1 Z " , " u p d a t e d A t " : " 2 0 1 3 - 1 1 - 0 6 T 0 4 : 0 7 : 1 9 . 2 7 1 Z " , " i d " : 1 } , { " n a m e " : " M i k e " , " p e t " : " S e a g u l l " , " c r e a t e d A t " : " 2 0 1 3 - 1 1 - 0 6 T 0 4 : 0 7 : 3 0 . 9 3 6 Z " , " u p d a t e d A t " : " 2 0 1 3 - 1 1 - 0 6 T 0 4 : 0 7 : 3 0 . 9 3 6 Z " , " i d " : 2 } . . .
  7. MODELVC User.js v a r U s e r =

    { a t t r i b u t e s : { n a m e : { t y p e : ' S T R I N G ' , r e q u i r e d : t r u e } , p e t : { t y p e : ' S T R I N G ' , r e q u i r e d : t r u e } } } m o d u l e . e x p o r t s = U s e r ; < b r >
  8. M VIEW C Support for all kinds of template engines.

    Works with Express? Works with Sails! Jade, ejs, M ustache, haml, handlebars etc. (Or you can just leave views out of Sails all together!)
  9. MV CONTROLLER C R U D Access to the Request

    and the Response objects like Express. Access to the Waterline ORM.
  10. THE (REAL-TIME) MVC FRAMEWORK Built in pubsub methods for controllers

    Customized Socket.io client methods (emulate POST, GET etc.) Session data passed with each socket request Subscribe clients to model changes
  11. MIDDLEWARE MAGIC! Still supports all the middleware for Express! Sails

    "Policies" to lock down your endpoints Passport connect-mongo connect-redis Mongoose Testing: Mocha qunit