GithubHelp home page GithubHelp logo

lab-express-routing's Introduction

Express Routing Lab

Introduction

Note: This can be a pair programming activity or done independently.

We'e now seen how to write an app with Node and Express from scratch, so let's apply this concept again by creating another RESTful API using Express.

A kid has come to you with an idea for an API to keep track of his candies. You love candies! So, you accept his proposal. You will create a resource Candy and use the kid's candy "data" to populate and post to your database. Check the additional resources at the bottom of this document to get some JSON data to add to your app as well, as well as some cURL examples to make sure your app works as expected!

Exercise

Requirements

  • Create an Express app from scratch
  • This app will only respond to JSON; it is just an API, so don't worry about the views
  • The resource Candy should be accessible via the endpoint /candies and be RESTful
  • Implement index,show, create, update, and destory functionality

Bonus:

  • Handle wrong responses with appropriate HTTP status and responses (404, 500, 422)
  • Add some validations for edit and update

Deliverable

An example app can be found in solution-code.

Once you spin up your local server, look below at the cURL commands curl cheat-sheet with the expected responses we want you to test on this app once you've finished building; the HTTP status should always be 2XX.

Index cURL Request

curl -XGET http://localhost:3000/candies
  • Expected Response
[{"id":1,"name":"Chewing Gum","color":"Red"},{"id":2,"name":"Pez","color":"Green"},{"id":3,"name":"Marshmallow","color":"Pink"},{"id":4,"name":"Candy Stick","color":"Blue"}]

Show cURL Request

curl -XGET http://localhost:3000/candies/3
  • Expected Response
{"id":3,"name":"Marshmallow","color":"Pink"}

Create cURL Request

curl -XPOST -H "Content-Type: application/json" -d '{"name":"Jelly Bear","color":"orange"}' http://localhost:3000/candies
  • Expected Response
    {"id":3,"name":"Marshmallow","color":"Pink"}

  • A second IndexcURL Request

    curl -XGET http://localhost:3000/candies
    • Expected Response
    [{"id":1,"name":"Chewing Gum","color":"Red"},{"id":2,"name":"Pez","color":"Green"},{"id":3,"name":"Marshmallow","color":"Pink"},{"id":4,"name":"Candy Stick","color":"Blue"},{"name":"Jelly Bear","color":"orange"}]

    Note: The new record is sent back !


Update cURL Request

curl -XPUT -H "Content-Type: application/json" -d '{"name":"Marshmallows","color":"white"}' http://localhost:3000/candies/3

Another Index Request

curl -XGET http://localhost:3000/candies
  • Expected Response
[{"id":1,"name":"Chewing Gum","color":"Red"},{"id":2,"name":"Pez","color":"Green"},{"name":"Marshmallows","color":"white"},{"id":4,"name":"Candy Stick","color":"Blue"}]

The record corresponding to the ID passed in the first request has been updated


Update cURL Request

curl -XDELETE http://localhost:3000/candies/2
  • Expected Response
    {"message":"deleted"}

Index cURL request, again!

curl -XGET http://localhost:3000/candies
  • Expected Response
[{"id":1,"name":"Chewing Gum","color":"Red"},null,{"id":3,"name":"Marshmallow","color":"Pink"},{"id":4,"name":"Candy Stick","color":"Blue"}]

Note: The record corresponding to the ID passed in the first request has been deleted.

Additional Resources

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.