Slide 1

Slide 1 text

RESTFUL API USING NODE.JS WITH EXPRESS

Slide 2

Slide 2 text

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.

Slide 3

Slide 3 text

AGENDA Quick overview of REST! Building REST API using Node with Express

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

WHAT IS REST?

Slide 6

Slide 6 text

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.

Slide 7

Slide 7 text

MAIN PRINCIPLES OF REST Identification of resources Manipulation of resources Self-descriptive messages

Slide 8

Slide 8 text

IDENTIFICATION OF RESOURCES !(CACHEABLE) !(SCALABLE) !(READABLE) YOU ARE DOING IT WRONG... :( /index.php?action=getarticle&id=5 /default/article/5/4/6/size

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

MANIPULATION OF RESOURCES Create Retrieve Update Delete But please note that REST != CRUD

Slide 12

Slide 12 text

MANIPULATION OF RESOURCES Create = POST Read = GET Update = PUT Delete = DELETE CRUD TO HTTP VERB MAPPING

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

SELF-DESCRIPTIVE MESSAGES Stateless! All information for processing is available: How? (method + content-type) What? (URI) When? (preconditions) Who? (authentication)

Slide 15

Slide 15 text

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)

Slide 16

Slide 16 text

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)

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

CODE DEMO

Slide 21

Slide 21 text

SUMMARY Be RESTful Only 2 URLs No verbs Use nouns as plurals Sweep complexity behind the ‘?’ Borrow from leading APIs

Slide 22

Slide 22 text

QUESTIONS!

Slide 23

Slide 23 text

THANK YOU!