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

RESTful API Using Node.js With Express

ravidsrk
October 12, 2015

RESTful API Using Node.js With Express

ravidsrk

October 12, 2015
Tweet

More Decks by ravidsrk

Other Decks in Programming

Transcript

  1. ABOUT ME RAVINDRA KUMAR Web Enthusiast, Open Source Addict FRONT

    END ENGINEER @ CLEARTRIP hacker, bug fixer, benchmark runner Email: ravidsrk AT gmail.com Twitter: @ravidsrk Github: /ravidsrk ravidsrk almost everywhere else.
  2. WARNING BEFORE WE START REST != MVC Do not think

    in controllers, id’s, actions, models, views, plugins,helpers etc... REST != CRUD *Caution: Not following advice will result in severe damage and makes me SAY: I TOLD YOU SO
  3. WHAT IS REST?? Roy Fielding (Guy who first defined REST

    in his dissertation on Architectural Styles) said: REST is a coordinated set of architectural constraints that attempts to minimize latency and network communication while at the same time maximizing the independence and scalability of component implementations. This is achieved by placing constraints on connector semantics where other styles have focused on component semantics.
  4. IDENTIFICATION OF RESOURCES !(CACHEABLE) !(SCALABLE) !(READABLE) YOU ARE DOING IT

    WRONG... :( /index.php?action=getarticle&id=5 /default/article/5/4/6/size
  5. IDENTIFICATION OF RESOURCES We want all articles We want the

    first comment of the fourth photo for the fifth article We want all comments of the fourth photo for the fifth article CACHEABLE! SCALABLE! READABLE! /articles /articles/5/photos/4/comments/1 /articles/5/photos/4/comments
  6. MANIPULATION OF RESOURCES Create = POST Read = GET Update

    = PUT Delete = DELETE CRUD TO HTTP VERB MAPPING
  7. MANIPULATION OF RESOURCES Resource POST (create) GET (read) PUT (update)

    DELETE (delete) /users create a new user list users bulk update users delete all users /users/1234 error show 1234 if exists update 1234, else error delete 1234
  8. SELF-DESCRIPTIVE MESSAGES Stateless! All information for processing is available: How?

    (method + content-type) What? (URI) When? (preconditions) Who? (authentication)
  9. SELF-DESCRIPTIVE MESSAGES GET /speaker/1234 HTTP/1.1 Host: www.jsfoo.com Accept: application/vnd.jsfoo.nl+xml ;

    version: 1.0 Authorization: OAuth oauth_nonce=”123” ... If-None-Matched: absad12412414 HOW (METHOD)
  10. SELF-DESCRIPTIVE MESSAGES GET /speaker/1234 HTTP/1.1 Host: www.jsfoo.com Accept: application/vnd.jsfoo.nl+xml ;

    version: 1.0 Authorization: OAuth oauth_nonce=”123” ... If-None-Matched: absad12412414 HOW (CONTENT-TYPE)
  11. SELF-DESCRIPTIVE MESSAGES GET /speaker/1234 HTTP/1.1 Host: www.jsfoo.com Accept: application/vnd.jsfoo.nl+xml ;

    version: 1.0 Authorization: OAuth oauth_nonce=”123” ... If-None-Matched: absad12412414 WHAT
  12. SELF-DESCRIPTIVE MESSAGES GET /speaker/1234 HTTP/1.1 Host: www.jsfoo.com Accept: application/vnd.jsfoo.nl+xml ;

    version: 1.0 Authorization: OAuth oauth_nonce=”123” ... If-None-Matched: absad12412414 WHEN
  13. SELF-DESCRIPTIVE MESSAGES GET /speaker/1234 HTTP/1.1 Host: www.jsfoo.com Accept: application/vnd.jsfoo.nl+xml ;

    version: 1.0 Authorization: OAuth oauth_nonce=”123” ... If-None-Matched: absad12412414 WHO
  14. SUMMARY Be RESTful Only 2 URLs No verbs Use nouns

    as plurals Sweep complexity behind the ‘?’ Borrow from leading APIs