Coder Social home page Coder Social logo

codaisseur-week5-rest-api-homework's Introduction

REST APIs Homework Assignment

This assignment is made up of two sections. The sections can be completed in any order. If you get stuck on one section, take a break and try another. Complete as many steps from each section as you can.

Note: Codaisseur uses the results of this homework assignment for a formal evaluation. You must write all of the code yourself. No collaboration or external help is allowed. Plagiarism is a violation of the Academy contract and is not in your best interest. Do not discuss the contents of the assignment with your fellow students.

How to submit your work

  1. Push your code to a GitHub repository.
  2. Send a link to the repository to [email protected] before Saturday 22:00

Setup

  1. Create a new local directory for this assignment.
  2. cd into that directory and create a new git repository. All files for this homework must be tracked in this repository.
  3. Initialize a Node.JS project in the repository directory so you can install and use packages.

Sections

1. Create an Express app with a single end-point.

  1. Create a new JS file named messages-api.js.

  2. Create an Express app in that file. The app should listen for requests on port 3000. Make sure you add the required dependency.

  3. Add a single endpoint to the app responds to POST requests to the /messages URI.

  4. When a request is sent to the endpoint, it should log the text property of the body to the console, and it should respond with a JSON object, for example:

    {
       "message": "This is the message that was sent"
    }

    In order to parse the JSON body of the request, you will need to add the middleware for it. Make sure you add the required dependency.

  5. Perform the following validation: if the body does NOT have a text property or the string is empty, then send a "Bad Request" HTTP status code to the client.

  6. The API should only log the message five times.

    After receiving five messages, sixth request should be sent a response that indicates the HTTP status for "Too Many Requests".

    Make sure the correct HTTP status code is sent (Google it if you haven't seen this status message before).

    Although there are libraries to implement such limits, do not use them! Implement the logic yourself.

  7. Put the message limit logic from the previous step into a middleware function. It should behave the same.

2. Use Sequelize to build a REST API.

  1. Create a new JavaScript file named sequelize-rest.js.

  2. Install the dependency [email protected]

  3. In the JavaScript file, initialize the database connection with Sequelize.

  4. Using Sequelize, define a model called Movie with the following properties (in addition to an ID):

    • title (text)
    • yearOfRelease (number)
    • synopsis (text)
  5. Make sure the model is synched with the database upon startup.

  6. Use the model create() method to insert 3 rows of example data. This logic should happen after the model synchronization completes. The data should persist. Restarting the API should not cause any data to be lost.

  7. Create an express app with routes that support the following RESTful actions on the "movies" resources.

    • create a new movie resource
    • read all movies (the collections resource)
    • read a single movie resource
    • update a single movie resource
    • delete a single movie resource

    You don't need any special logic. A standard REST implementation is ok.

  8. Make sure that your handlers send back 404 status codes when appropriate.

  9. Implement pagination on the "read all" collections resource end-point.

    The user must be able to pass limit and offset as query parameters to correctly control what results they receive. You can access query parameters on the req.query object. Sequelize supports pagination through the findAndCountAll query method.

    The response should, in addition to the array of resources, also contain a number indicating how many results there are in total. So, it should look like this:

    {
       data: [
          { ... },
          { ... },
          ...
       ],
       total: 12
    }
  10. Make sure that all endpoints handle database errors in the promise chain. Errors should be handled by Express' built-in error handler.

codaisseur-week5-rest-api-homework's People

Contributors

jendhordejan avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

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.