How to quickly make REST APIs with CompoundJS

2,420 views

Published on

Presentation of the backend features provided by CompoundJS a full-featured web framework for Node.js

http://compoundjs.com/
https://github.com/1602/compound/

Talk performed @ LyonJS Meetup, April 2013

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

No Downloads
Views
Total views
2,420
On SlideShare
0
From Embeds
0
Number of Embeds
1,386
Actions
Shares
0
Downloads
9
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

How to quickly make REST APIs with CompoundJS

  1. 1. How to quickly make REST APIs
  2. 2. infosmade by Anatoliy Chakkaev1208 stars on github1000+ commitspreviously named RailwayJS
  3. 3. based on top of ExpressJSExpress is inspired by Sinatra: simple and light  Express has cool stuff: middlewares, error  handling, logging...
  4. 4. an express serverexpress = require expressapp = express()app.get /hello, (req, res) -> res.end hello: world
  5. 5. Neat but almost nothing is configured by default.
  6. 6. compoundinspired by Rails: full featured and scaffolding  quickstartcompound init blog-api --coffee
  7. 7. configurationLoad dependenciescd blog-apinpm installBackend only: we remove UIrm -rf public app/assets app/views# remove code from config/environment.coffee- app.use compound.assetsCompiler.init()- app.set cssEngine, stylus- app.use express.static #{app.root}/public, maxAge: 86400000
  8. 8. our filesapp/app/models/app/controllers/app/controllers/application_controller.coffeeapp/observers/app/helpers/db/db/schema.coffeelog/node_modules/config/config/locales/config/initializers/config/environments/config/environment.coffeeconfig/environments/development.coffeeconfig/environments/production.coffeeconfig/environments/test.coffeeconfig/initializers/db-tools.coffeeconfig/routes.coffeeconfig/autoload.coffeeconfig/database.coffeeserver.coffeeREADME.mdpackage.json
  9. 9. routesapp/app/models/app/controllers/app/controllers/application_controller.coffeeapp/observers/app/helpers/db/db/schema.coffeelog/node_modules/config/config/locales/config/initializers/config/environments/config/environment.coffeeconfig/environments/development.coffeeconfig/environments/production.coffeeconfig/environments/test.coffeeconfig/initializers/db-tools.coffeeconfig/routes.coffeeconfig/autoload.coffeeconfig/database.coffeeserver.coffeeREADME.mdpackage.json
  10. 10. controllersapp/app/models/app/controllers/app/controllers/application_controller.coffeeapp/observers/app/helpers/db/db/schema.coffeelog/node_modules/config/config/locales/config/initializers/config/environments/config/environment.coffeeconfig/environments/development.coffeeconfig/environments/production.coffeeconfig/environments/test.coffeeconfig/initializers/db-tools.coffeeconfig/routes.coffeeconfig/autoload.coffeeconfig/database.coffeeserver.coffeeREADME.mdpackage.json
  11. 11. modelsapp/app/models/app/controllers/app/controllers/application_controller.coffeeapp/observers/app/helpers/db/db/schema.coffeelog/node_modules/config/config/locales/config/initializers/config/environments/config/environment.coffeeconfig/environments/development.coffeeconfig/environments/production.coffeeconfig/environments/test.coffeeconfig/initializers/db-tools.coffeeconfig/routes.coffeeconfig/autoload.coffeeconfig/database.coffeeserver.coffeeREADME.mdpackage.json
  12. 12. log, dependencies, infosapp/app/models/app/controllers/app/controllers/application_controller.coffeeapp/observers/app/helpers/db/db/schema.coffeelog/node_modules/config/config/locales/config/initializers/config/environments/config/environment.coffeeconfig/environments/development.coffeeconfig/environments/production.coffeeconfig/environments/test.coffeeconfig/initializers/db-tools.coffeeconfig/routes.coffeeconfig/autoload.coffeeconfig/database.coffeeserver.coffeeREADME.mdpackage.json
  13. 13. configapp/app/models/app/controllers/app/controllers/application_controller.coffeeapp/observers/app/helpers/db/db/schema.coffeelog/node_modules/config/config/locales/config/initializers/config/environments/config/environment.coffeeconfig/environments/development.coffeeconfig/environments/production.coffeeconfig/environments/test.coffeeconfig/initializers/db-tools.coffeeconfig/routes.coffeeconfig/autoload.coffeeconfig/database.coffeeserver.coffeeREADME.mdpackage.json
  14. 14. start + commandsapp/app/models/app/controllers/app/controllers/application_controller.coffeeapp/observers/app/helpers/db/db/schema.coffeelog/node_modules/config/config/locales/config/initializers/config/environments/config/environment.coffeeconfig/environments/development.coffeeconfig/environments/production.coffeeconfig/environments/test.coffeeconfig/initializers/db-tools.coffeeconfig/routes.coffeeconfig/autoload.coffeeconfig/database.coffeeserver.coffeeREADME.mdpackage.json
  15. 15. models and JugglingDBORM/ODM for Node.jsIntegrated with Compoundadaptable to any data sourcevalidatorsrelations
  16. 16. generatorcompound g model post title content published:booleanrenders:# db/schema.coffeePost = describe Post, -> property title, String property content, String property published, Boolean# app/models/post.coffeemodule.exports = (compound, Post) →lets add this: Post.published = (callback) -> Post.all published: true, callback
  17. 17. controllers# app/controllers/posts_controller.coffeeaction create, -> Post.create body (err, post) -> if err then send 500 else send post, 201action all, -> Post.all (err, posts) -> if err then send 500 else send postsaction published, -> Post.published (err, posts) -> if err then send 500 else send posts
  18. 18. pre-processors and post-processorsbefore -> Post.find params.id, (err, post) => if err then send 500 else if not post then send 404 else @post = post next(), only: [show, update, destroy]after -> console.log request processed next()
  19. 19. That makes simpler controllers!  action show, -> send @postaction update, -> @post.updateAttributes body, (err) -> if err then send 500 else send 200action destroy, -> @post.destroy -> if err then send 500 else send 204
  20. 20. routing helpersbasic# config/routes.coffeemap.get posts, posts#publishedmap.get admin/posts, posts#allmap.post admin/posts, posts#createmap.get admin/posts/:id, posts#showmap.put admin/posts/:id, posts#modifymap.del admin/posts/:id, posts#destroyevolved# generate standard CRUD routesmap.resources posts# Link directly to controller with the right namemap.all :controller/:actionmap.all :controller/:action/:id
  21. 21. namespacesmap.get posts, posts#publishedmap.namespace admin, -> map.get posts, posts#all map.post posts, posts#create map.get posts/:id, posts#show map.put posts/:id, posts#modify map.del posts/:id, posts#destroy
  22. 22. moregenerators for controllersSoon: custom generators  Soon: custom structure  + a lof of features if you want to make UIs
  23. 23. a talk by...contact@cozycloud.cchttps://blog.cozycloud.cchttps://twitter.com/mycozycloudCrédits photos: blmiers2, Ethan AblemanLicense Creative Commons by-3.0

×