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

RESTful API Using Node.js With Express

E3dd6e14f351f38c98caefdde9eb61e6?s=47 ravidsrk
October 12, 2015

RESTful API Using Node.js With Express

E3dd6e14f351f38c98caefdde9eb61e6?s=128

ravidsrk

October 12, 2015
Tweet

Transcript

  1. RESTFUL API USING NODE.JS WITH EXPRESS

  2. 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.
  3. AGENDA Quick overview of REST! Building REST API using Node

    with Express
  4. 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
  5. WHAT IS REST?

  6. 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.
  7. MAIN PRINCIPLES OF REST Identification of resources Manipulation of resources

    Self-descriptive messages
  8. IDENTIFICATION OF RESOURCES !(CACHEABLE) !(SCALABLE) !(READABLE) YOU ARE DOING IT

    WRONG... :( /index.php?action=getarticle&id=5 /default/article/5/4/6/size
  9. 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
  10. IDENTIFICATION OF RESOURCES ✗ /photos/order/size/limit/5 ✗ /photos/limit/5/order/size ✓ /photos?order=size&limit=5 ✓

    /photos?limit=5&order=size FILTERING THROUGH A QUERY STRING, NOT THE URI
  11. MANIPULATION OF RESOURCES Create Retrieve Update Delete But please note

    that REST != CRUD
  12. MANIPULATION OF RESOURCES Create = POST Read = GET Update

    = PUT Delete = DELETE CRUD TO HTTP VERB MAPPING
  13. 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
  14. SELF-DESCRIPTIVE MESSAGES Stateless! All information for processing is available: How?

    (method + content-type) What? (URI) When? (preconditions) Who? (authentication)
  15. 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)
  16. 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)
  17. 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
  18. 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
  19. 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
  20. CODE DEMO

  21. SUMMARY Be RESTful Only 2 URLs No verbs Use nouns

    as plurals Sweep complexity behind the ‘?’ Borrow from leading APIs
  22. QUESTIONS!

  23. THANK YOU!