24
How to quickly make REST APIs

How to quickly make REST APIs with CompoundJS

  • Upload
    gelnior

  • View
    1.654

  • Download
    2

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: How to quickly make REST APIs with CompoundJS

How to quickly make REST APIs

Page 2: How to quickly make REST APIs with CompoundJS

infos

made by Anatoliy Chakkaev1208 stars on github1000+ commitspreviously named RailwayJS

Page 3: How to quickly make REST APIs with CompoundJS

based on top of ExpressJS

Express is inspired by Sinatra: simple and light Express has cool stuff:   middlewares, error handling, logging...

Page 4: How to quickly make REST APIs with CompoundJS

an express server

express = require 'express'app = express()

app.get '/hello', (req, res) -> res.end hello: 'world'

Page 5: How to quickly make REST APIs with CompoundJS

Neat but almost nothing is configured by default.

Page 6: How to quickly make REST APIs with CompoundJS

compound

inspired by Rails:   full featured and scaffolding

quickstart

compound init blog-api --coffee

Page 7: How to quickly make REST APIs with CompoundJS

configuration

Load dependencies

cd blog-apinpm install

Backend only: we remove UI

rm -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

Page 8: How to quickly make REST APIs with CompoundJS

app/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

our files

Page 9: How to quickly make REST APIs with CompoundJS

app/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

routes

Page 10: How to quickly make REST APIs with CompoundJS

app/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

controllers

Page 11: How to quickly make REST APIs with CompoundJS

app/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

models

Page 12: How to quickly make REST APIs with CompoundJS

app/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

log, dependencies, infos

Page 13: How to quickly make REST APIs with CompoundJS

app/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

config

Page 14: How to quickly make REST APIs with CompoundJS

app/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

start + commands

Page 15: How to quickly make REST APIs with CompoundJS
Page 16: How to quickly make REST APIs with CompoundJS

models and JugglingDB

ORM/ODM for Node.jsIntegrated with Compoundadaptable to any data sourcevalidatorsrelations

Page 17: How to quickly make REST APIs with CompoundJS

generator

compound g model post title content published:boolean

renders:

# db/schema.coffeePost = describe 'Post', -> property 'title', String property 'content', String property 'published', Boolean

# app/models/post.coffeemodule.exports = (compound, Post) →

let's add this: Post.published = (callback) -> Post.all published: true, callback

Page 18: How to quickly make REST APIs with CompoundJS

controllers

# app/controllers/posts_controller.coffee

action 'create', -> Post.create body (err, post) -> if err then send 500 else send post, 201

action 'all', -> Post.all (err, posts) -> if err then send 500 else send posts

action 'published', -> Post.published (err, posts) -> if err then send 500 else send posts

Page 19: How to quickly make REST APIs with CompoundJS

pre-processors and post-processors

before -> 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()

Page 20: How to quickly make REST APIs with CompoundJS

That makes simpler controllers! 

action 'show', -> send @post

action 'update', -> @post.updateAttributes body, (err) -> if err then send 500 else send 200

action 'destroy', -> @post.destroy -> if err then send 500 else send 204

Page 21: How to quickly make REST APIs with CompoundJS

routing helpers

basic

# config/routes.coffeemap.get 'posts', 'posts#published'map.get 'admin/posts', 'posts#all'map.post 'admin/posts', 'posts#create'map.get 'admin/posts/:id', 'posts#show'map.put 'admin/posts/:id', 'posts#modify'map.del 'admin/posts/:id', 'posts#destroy'

evolved

# generate standard CRUD routesmap.resources 'posts'

# Link directly to controller with the right namemap.all ':controller/:action'map.all ':controller/:action/:id'

Page 22: How to quickly make REST APIs with CompoundJS

namespaces

map.get 'posts', 'posts#published'map.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'

Page 23: How to quickly make REST APIs with CompoundJS

more

generators for controllersSoon: custom generators Soon: custom structure + a lof of features if you want to make UIs

Page 24: How to quickly make REST APIs with CompoundJS

[email protected]://blog.cozycloud.cchttps://twitter.com/mycozycloud

Crédits photos: blmiers2, Ethan Ableman

License Creative Commons by-3.0

a talk by...