kitchen-boy / zookeepr

A web server for a front-end application by the local zoo called Zoo Keepr
0 stars 0 forks source link

Create a web server #1

Open kitchen-boy opened 2 years ago

kitchen-boy commented 2 years ago

User Stories

kitchen-boy commented 2 years ago

11.1.1 Intro:

Lesson Objectives:

kitchen-boy commented 2 years ago

11. 1.2. Preview:

kitchen-boy commented 2 years ago

11.1.3 Set Up the Project:

kitchen-boy commented 2 years ago

11.1.4. Create an Express.js Server:

To instantiate the server, add the following code to <server.js>:

Instantiate the server

Note:

kitchen-boy commented 2 years ago

11.1.5 Handle Requests for Animals:

Create a route that the front-end can request data from.

Start by requiring the data

Note 2 important things about this code:

  1. The <get()> method requires 2 arguments: --i) a string that describes the route the client will have to fetch from. --ii) a callback function that will execute every time that route is accessed with a GET request.
  2. We are using the <send()> method rom the <res> parameter (short for response) to send the string <Hello!> to our client.

Save the code.

Run <npm start> to start the server again.

Update the code to use <res.json()>:

To create specifying functionality with our own server:

Access the <query> property on the <req> object:

Access the query property on the req object

Create a new function called <filterByQuery()> by adding the following code, above the <.get()>:

-- Going to break the filter functionality into its own function, instead of handling the filter functionality inside the <.get()> callback. filterByQuery() function -- This function will take in <req.query> as an argument & filter through the animals accordingly, returning the new filtered array.

Call the <filterByQuery()> function in the <app.get()> callback * as shown in the following code:

Calling  filterByQuery()  in the  app get()  callback

Next question: How to filter out animals based on their personality traits?

-- Different filter and more complex: -> because the personality traits are within an array. -> users might want to filter animals by multiple personality traits at the same time. ==> Will have to handle <query.personalityTraits> differently from the way we handle the other queries.

How does this process differ when querying for one versus multiple personality traits?

To combat this, modify the filterByQuery() function to handle both cases:

*Add the following code: Modified  filterByQuery()  to hand both array:string

Now: the <filterByQuery()> method ensures that <query.personalityTraits> is always an array before the <.forEach()**> method executes.

*Test the route again in the browser: -with personalityTraits <hungry> and <zany>.

kitchen-boy commented 2 years ago

11.1.6 Deploy to Heroku:

We need to use Heroku because:

1. Create a Heroku account at Heroku

2. Download the Heroku CLI (Command Line Interface).

To create a new Heroku application:

Heroku doesn't allow for cross-branch actions the same way GitHub allows.

Run the following commands from the command line:

git push commands to heroku

DONE: Created app with Heroku!

Open your newly created app in the browser using the output from the command line:

Output after heroku create command -- 2 items in the output from the command line that look like links:

  1. <your-app-name>.herokuapp.com => Open this link in the browser

  2. Do NOT open the one that ends with <.git>.

    OR:

    Open the application by entering <heroku open> from the command line.

Tell our app to use an environment variable instead of hardcoding our app to use 80.

When Heroku runs our app, it sets an environment variable called:

environment variable process env PORT -- Tell our app to use that port if it has been set, and if not, default to port 80.

IMPORTANT:

Applications served over Heroku as well as most hosts must run on port 80. If the host uses HTTPS, then the port would be set to 443.

Add this code before , in :

environment variable = (process env PORT)

Replace the hardcoded value with the variable:

-- Go down to where we have our application listening> Replace hardcoded value with PORT variable

Add & commit your application.

Push to <heroku feature/MVP:main>:

git push commands to heroku -- Need to add, commit & push changes to <heroku feature/MVP:main> before app can be successfully opened in the browser. -- Open the app by entering <heroku open> from the command line!

kitchen-boy commented 2 years ago

11.1.7 Handle Requests for a Specific Animal: