Skip to content

Latest commit

 

History

History
169 lines (121 loc) · 5.15 KB

5-api-express.md

File metadata and controls

169 lines (121 loc) · 5.15 KB

⤵️ Step 5 - Request the Products

How to give access to your data

Table of Contents

🎯 Objective

Build an api with Express to request data from your database...

🏗 Prerequisites

  1. Be sure to have a clean working copy.

This means that you should not have any uncommitted local changes.

cd /path/to/workspace/clear-fashion
❯ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
  1. Pull the master branch to update your local with the new remote changes
❯ git remote add upstream [email protected]:92bondstreet/clear-fashion.git
## or ❯ git remote add upstream https://github.com/92bondstreet/clear-fashion
❯ git fetch upstream
❯ git pull upstream master
  1. Check the terminal output for the command node sandbox-db.js
cd /path/to/workspace/clear-fashion/server
## install new dependencies
❯ yarn
## or ❯ npm install
❯ node sandbox-db.js

  1. Check the terminal output for the command node api.js
cd /path/to/workspace/clear-fashion/server
## install new dependencies
❯ yarn
## or ❯ npm install
❯ node api.js

⤵️ List of endpoints to implement

GET /products/:id

Fetch a specific product.

❯ curl -H "Accept: application/json" http:https://localhost:8092/products/f9360699-2c7d-5cec-8529-374d3e166f87
{
    "_id": "f9360699-2c7d-5cec-8529-374d3e166f87",
    "link": "https://www.loom.fr/collections/hauts/products/le-t-shirt",
    "brand": "loom",
    "price": 20,
    "name": "Le t-shirt en coton",
    "photo":"//cdn.shopify.com/s/files/1/1355/7899/products/loom_tshirt_coton_gris_anthracite_face_2_394x.jpg?v=1611741180"
}

GET /products/search

Search for specific products

This endpoint accepts the following optional query string parameters:

  • limit - number of products to return (default: 12)
  • brand - filter by brand (default: All brands)
  • price - filter by price (default: All price)

The results array should be sorted by price in ascending way.

❯ curl -H "Accept: application/json" http:https://localhost:8092/products/search?limit=5&brand=loom&price=30
{
  "limit": 5,
  "total": 2,
  "results": [
    {
        "_id": "f9360699-2c7d-5cec-8529-374d3e166f87",
        "link": "https://www.loom.fr/collections/hauts/products/le-t-shirt",
        "brand": "loom",
        "price": 20,
        "name": "Le t-shirt en coton",
        "photo":"//cdn.shopify.com/s/files/1/1355/7899/products/loom_tshirt_coton_gris_anthracite_face_2_394x.jpg?v=1611741180"
    },
    {
        "_id": "8ad2f7ed-652f-57a9-b48e-659b8e4fa3d2",
        "link": "https://www.loom.fr/collections/hauts/products/le-t-shirt-en-coton-bio",
        "brand": "loom",
        "price": 25},
        "name": "Le t-shirt en coton bio",
        "photo": "//cdn.shopify.com/s/files/1/1355/7899/products/tshirtloomcotonbioblancface2_394x.jpg?v=1611740926"
    }
  ]
}

👩‍💻 Just tell me what to do

  1. Install the desktop API client Insomnia

  2. Check the API endpoint / with insomnia

  1. Implement the endpoints

  2. Commit your modification

cd /path/to/workspace/clear-fashion
❯ git add -A && git commit -m "feat(get-product): get a specific product"

(why following a commit message convention?)

  1. Commit early, commit often
  2. Don't forget to push before the end of the workshop
❯ git push origin master

Note: if you catch an error about authentication, add your ssh to your github profile.

If you need some helps on git commands, read git - the simple guide

📦 Suggested node modules

  • dotenv - Loads environment variables from .env for nodejs projects
  • express - Fast, unopinionated, minimalist web framework for node
  • nodemon - Monitor for any changes in your node.js application and automatically restart the server - perfect for development

🛣️ Related Theme and courses