How to quickly make REST APIs
infos

made by Anatoliy Chakkaev
1208 stars on github
1000+ commits
previously named RailwayJS
based on top of ExpressJS


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

express = require 'express'
app = express()

app.get '/hello', (req, res) ->
  res.end hello: 'world'
Neat but almost nothing is configured by default.
compound

inspired by Rails: full featured and scaffolding
                  

quickstart
compound init blog-api --coffee
configuration

Load dependencies
cd blog-api
npm 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
our files
app/
app/models/
app/controllers/
app/controllers/application_controller.coffee
app/observers/
app/helpers/
db/
db/schema.coffee
log/
node_modules/
config/
config/locales/
config/initializers/
config/environments/
config/environment.coffee
config/environments/development.coffee
config/environments/production.coffee
config/environments/test.coffee
config/initializers/db-tools.coffee
config/routes.coffee
config/autoload.coffee
config/database.coffee
server.coffee
README.md
package.json
routes
app/
app/models/
app/controllers/
app/controllers/application_controller.coffee
app/observers/
app/helpers/
db/
db/schema.coffee
log/
node_modules/
config/
config/locales/
config/initializers/
config/environments/
config/environment.coffee
config/environments/development.coffee
config/environments/production.coffee
config/environments/test.coffee
config/initializers/db-tools.coffee
config/routes.coffee
config/autoload.coffee
config/database.coffee
server.coffee
README.md
package.json
controllers
app/
app/models/
app/controllers/
app/controllers/application_controller.coffee
app/observers/
app/helpers/
db/
db/schema.coffee
log/
node_modules/
config/
config/locales/
config/initializers/
config/environments/
config/environment.coffee
config/environments/development.coffee
config/environments/production.coffee
config/environments/test.coffee
config/initializers/db-tools.coffee
config/routes.coffee
config/autoload.coffee
config/database.coffee
server.coffee
README.md
package.json
models
app/
app/models/
app/controllers/
app/controllers/application_controller.coffee
app/observers/
app/helpers/
db/
db/schema.coffee
log/
node_modules/
config/
config/locales/
config/initializers/
config/environments/
config/environment.coffee
config/environments/development.coffee
config/environments/production.coffee
config/environments/test.coffee
config/initializers/db-tools.coffee
config/routes.coffee
config/autoload.coffee
config/database.coffee
server.coffee
README.md
package.json
log, dependencies, infos
app/
app/models/
app/controllers/
app/controllers/application_controller.coffee
app/observers/
app/helpers/
db/
db/schema.coffee
log/
node_modules/
config/
config/locales/
config/initializers/
config/environments/
config/environment.coffee
config/environments/development.coffee
config/environments/production.coffee
config/environments/test.coffee
config/initializers/db-tools.coffee
config/routes.coffee
config/autoload.coffee
config/database.coffee
server.coffee
README.md
package.json
config
app/
app/models/
app/controllers/
app/controllers/application_controller.coffee
app/observers/
app/helpers/
db/
db/schema.coffee
log/
node_modules/
config/
config/locales/
config/initializers/
config/environments/
config/environment.coffee
config/environments/development.coffee
config/environments/production.coffee
config/environments/test.coffee
config/initializers/db-tools.coffee
config/routes.coffee
config/autoload.coffee
config/database.coffee
server.coffee
README.md
package.json
start + commands
app/
app/models/
app/controllers/
app/controllers/application_controller.coffee
app/observers/
app/helpers/
db/
db/schema.coffee
log/
node_modules/
config/
config/locales/
config/initializers/
config/environments/
config/environment.coffee
config/environments/development.coffee
config/environments/production.coffee
config/environments/test.coffee
config/initializers/db-tools.coffee
config/routes.coffee
config/autoload.coffee
config/database.coffee
server.coffee
README.md
package.json
models and JugglingDB

ORM/ODM for Node.js
Integrated with Compound
adaptable to any data source
validators
relations
generator
compound g model post title content published:boolean

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

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

let's add this:
 Post.published = (callback) ->
   Post.all published: true, callback
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
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()
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
routing helpers

basic
# config/routes.coffee
map.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 routes
map.resources 'posts'

# Link directly to controller with the right name
map.all ':controller/:action'
map.all ':controller/:action/:id'
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'
more

generators for controllers
Soon: custom generators
       
Soon: custom structure
         
+ a lof of features if you want to make UIs
a talk by...




contact@cozycloud.cc
https://blog.cozycloud.cc
https://twitter.com/mycozycloud



Crédits photos: blmiers2, Ethan Ableman
License Creative Commons by-3.0

How to quickly make REST APIs with CompoundJS