Skip to content

Instantly share code, notes, and snippets.

@remilapeyre
Last active January 21, 2019 13:24
Show Gist options
  • Save remilapeyre/4a454b32c213eb588bacba1cbf9d45b3 to your computer and use it in GitHub Desktop.
Save remilapeyre/4a454b32c213eb588bacba1cbf9d45b3 to your computer and use it in GitHub Desktop.

TODO List

  1. Create a simple HTTP services that responds with Hello world to every requests:
const http = require('http')
const port = 3000

let test = []

const requestHandler = (request, response) => {
  console.log(request.url)
  response.end(`Hello world!`)
}

const server = http.createServer(requestHandler)

server.listen(port, (err) => {
  if (err) {
    return console.log('something bad happened', err)
  }

  console.log(`server is listening on ${port}`)
})

  1. Check that this service works with Postman.

  2. Change the previous service so it responds with Hello world only on the /hello_route and returns an error with a 404 status code on every other route.

  3. This is an application for a TODO list. You must add endpoints that will:

    • retrieve the list of tasks /tasks/

      [
          {
              "completed": false,
              "title": "My task",
              "content" Implement API",
              "due_date": "2016-01-04",
              "created_date": "2019-01-02
              "completion_date": null,
              "id": 1234,
          },
          ...
      ]
      
    • get details about a given task /tasks/1234

      {
          "completed": false,
          "title": "My task",
          "content" Implement API",
          "due_date": "2016-01-04",
          "created_date": "2019-01-02"
          "completion_date": null,
          "id": 1234,
          "tags": [
              "school"
          ],
          "order": 123,
          "priority": 1,
          "project_id": 2345,
      }
      
  4. Add an endpoint to delete a task. What should be the URL and the verb used for this?

  5. We want to be able to create new tasks, what should be the verb used for that action? Implement this endpoint and test it.

To implement this, you will need to read the body of the request, check out https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/#request-body

  1. We want to be able to mark the task as completed without changing its attributes. How can we do this?

  2. We now want to attach tasks to users and to projects. What changes need to be made to the current API? Implement them.

  3. So far, the user is obligated to create the API endpoints by hand before calling them. Is there another way?

Blog

  1. We want to create a blog and its API. The resources should be:
    • authors
    • articles
    • comments
    • images
    • tags

We need to be able to create, read, update and delete every resources.

  1. What should /authors/12/articles return? Implement it.

HTTP API client

An API is good only when it is used by others. You must build a client for every API you use. Have a look at Node HTTPS module and implement one for each API you did.

Next

Anybody can post and update anything on our API, we must protect it with authentication.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment