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

Beer Locker - Building a RESTful API With Node

Beer Locker - Building a RESTful API With Node

With an ever growing collection of beer, I am in dire need of a way to store and track it. What better way to do this than to create an application.

This will be a hands on live coding session where we will create a RESTful API using Node, Express, Mongoose, and MongoDB.

When we are done, we should have an API that supports CRUD operations, have a solid understanding of best practices and tooling, user accounts, authentication via Passport, and most important of all, a place to store our beer!

Scott Smith

August 01, 2014
Tweet

More Decks by Scott Smith

Other Decks in Programming

Transcript

  1. Prerequisites ™  Node and npm are installed ™  MongoDB is

    installed and running ™  Love APIs and Beer
  2. Agenda ™  REST ™  Basic express application ™  Tooling ™ 

    MongoDB, Mongoose and models ™  Create, read, update, & delete endpoints (CRUD) ™  Passport overview ™  Add User accounts ™  Build Auth controller ™  Lockdown CRUD endpoints
  3. REST – Representational State Transfer ™  Style of architecture for

    web services. ™  De facto web service design model. ™  Displaced other design models such as SOAP and WSDL due to its simpler style.
  4. RESTful Web Services A RESTful web service is a web

    service implemented using HTTP and the principles of REST. It is a collection of resources, with four defined aspects: ™  The base URI for the web service such as http://site.com/api ™  The content type of the data supported by the web service. This could be JSON, XML, or any other valid Internet media type ™  The set of operations supported by the web service such as GET, PUT, POST, or DELETE. ™  The API must be hypertext driven.
  5. CRUD ™ Create POST/cars HTTP/1.1 ™ Read GET /cars HTTP/1.1 GET /cars/tesla

    HTTP/1.1 ™ Update PUT /cars/tesla HTTP/1.1 ™ Delete DELETE /cars/ford HTTP/1.1
  6. Postman ™  Postman is a powerful HTTP client to help

    test web services easily and efficiently. It lets you craft simple as well as complex HTTP requests quickly.
  7. MongoDB and Mongoose ™  Load the Mongoose package ™  Connect

    to MongoDB using Mongoose ™  Create our Beer model
  8. Accept POST & PUT data ™  Load the body-parser middleware

    ™  Use the body-parser middleware
  9. Add beer to our locker ™  Create a new Express

    route at /beers ™  Create a POST endpoint on new route ™  Create a Mongoose Beer model ™  Set Beer model data from POST request ™  Save the Beer model to MongoDB
  10. Party time, get all the beer! ™  Create a GET

    endpoint on /beers route ™  Use the Mongoose Beer model to get all beer
  11. Pace yourself with a beer ™  Create a new Express

    route at /beer/:beer_id ™  Create a GET endpoint on new route ™  Use the Mongoose Beer model to find the right beer
  12. Updating beer quantity ™  Create a PUT endpoint on /beer/:beer_id

    route ™  Use the Mongoose Beer model to find the right beer ™  Use the Mongoose Beer model to update the right beer
  13. I drank my last beer! ™  Create a DELETE endpoint

    on /beer/:beer_id route ™  Use the Mongoose Beer model to find the right beer ™  Use the Mongoose Beer model to remove the right beer
  14. Passport Official description: “Authentication middleware for Node.js. Extremely flexible and

    modular, Passport can be unobtrusively dropped in to any Express-based web application. A comprehensive set of strategies support authentication using a username and password, Facebook, Twitter, and more.”
  15. Passport Features ™  140+ authentication strategies ™  Single sign-on with

    OpenID and Oauth ™  Supports persistent sessions ™  Pick and choose required strategies ™  Create your own strategies
  16. User Model and Controller ™  Create the User model ™ 

    Create the User controller ™  Route to our User controller endpoints
  17. Don’t touch my beer! ™  Update Beer model to define

    ownership ™  Update Beer controller to use ownership