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

Build a RESTful API with Node in 10 Minutes

Build a RESTful API with Node in 10 Minutes

Presents as a Lightning Talk at Cardinal Solutions Group, Charlotte, NC, on April 25, 2017

vjwilson

April 25, 2017
Tweet

More Decks by vjwilson

Other Decks in Programming

Transcript

  1. Why an API? Using a RESTful API as a web

    service allows you to have a single source of truth for your data that you can consume in many different ways. • Website (with either client-side or server-side front-end) • Hybrid mobile app • Native mobile app • Desktop app • Command-line app • Other Services (e.g., Jira, Bamboo, and GitHub APIs can all talk to each other)
  2. REST Perform requests and receive responses via HTTP Stateless Client-server

    Cacheable Resource-first HTTP verbs correspond to actions
  3. HTTP Protocol Easy to use, Easy to test HTTP library

    for any language Test GET and POST right in a browser REST Client (Postman, Paw) Use on command line (curl, wget)
  4. Why Node? Node.js uses an event-driven, non-blocking I/O model that

    makes it lightweight and efficient. Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world. • Built-in http package (low-level, roll your own) • Express (de facto standard) • Koa, or thousands of other new contenders (it is JavaScript, remember?) https://www.npmjs.com/search?q=http => 27922 packages found for "http"
  5. CRUD Create - Read - Update - Delete At a

    basic level, it’s easy to use a REST API to implement CRUD for a resource.
  6. Rule of thumb for the example app Just the first

    of these three goals... • Make It Work • Make It Right • Make It Fast
  7. CREATE HTTP POST verb Request URL: /recipes Action: POST Payload:

    { "name": "Pie" } Response Status: 201 Body: { "name": "Pie", "id": 1 }
  8. READ HTTP GET verb Request URL: /recipes Action: GET Payload:

    (none) Response Status: 200 Body: [ { "name": "Pie", "id": 1 }, { "name": "Cake", "id": 2 } ]
  9. READ (just one) HTTP GET verb Request URL: /recipes/:recipeId e.g.,

    /recipes/2 Action: GET Payload: (none) Response Status: 200 Body: { "name": "Cake", "id": 2 }
  10. UPDATE HTTP PUT verb Request URL: /recipes/:recipeId e.g., /recipes/1 Action:

    PUT Payload: { "name": "Pecan Pie" } Response Status: 200 Body: { "name": "Pecan Pie", "id": 1 }
  11. DELETE HTTP DELETE verb Request URL: /recipes/:recipeId e.g., /recipes/1 Action:

    DELETE Payload: (none) Response Status: 204 Body: (none)