Open kitchen-boy opened 2 years ago
Zoo Keepr application - front end:
Lesson objective = finish building the back end (i.e., server), then connect it to the front-end code.
This will allow the front end to make AJAX requests to the server.
AJAX = Asynchronous JavaScript And XML.
AJAX uses a combination of: -- A browser built-in XMLHttpRequest object (to request data from a web server) --JavaScript and HTML DOM (to display or use the data). With AJAX, you can:
Update a web page without reloading the page
Request data from a server - after the page has loaded
Receive data from a server - after the page has loaded
Send data to a server - in the background
Create the project's repo in GitHub & establish the feature branches using issues.
Use a library called Express.js to create a Node-based web server.
Use Express.js to create an HTTP route that sends the requestor all of the animal data as JSON.
Will get introduced to Heroku early on for this project so that we can continuously deploy the server as we go.
Will create one more API route that allows us to search for one specific animal in the array.
Need to set up Express.js, one of the industry-leading libraries for creating web servers in Node.js, before we can create the routes for our API to handle requests.
Need to deploy the app to Heroku because GitHub Pages doesn't support Node applications. Choosing to deploy Heroku earlier so that we can continuously deploy as we work on the project -- even though we could deploy to Heroku at end of this lesson.
Note:
Once the server has started: -- navigate to http://localhost:3001/api/animals in your browser. -- Should see something like following image:
Use <send()> method to send short messages.
To send lots of JSON (e.g. from APIs), change <send> to <json>. -- Do this to change the headers (i.e. additional metadata that's sent with every request/response) so that hte client knows it's receiving JSON.
(e.g.. To specify parameters with your query by using <?> or <:
Add another function called <filterByQuery()> to create this query. -- This is going to help to handle different kinds of queries. -- Start by extracting the data from after the question mark. -- Use the <req> parameter, which is short for "request".
Note: Express.js documentation on Request -- the <req> object gives us access to much more than just the <.query> property (just like the <res> object).
Navigate to http://localhost:3001/api/animals?name=Erica in the browser. -- Check the command line after that page loads. -- Should see an object with the property <name> & value <Erica> as shown:
Whatever string of query parameters you use on the URL will become JSON: -- i.e. ?name=Erica -- e.g. ?a=111&b=222$c=333 will become: -- e.g. ?a=111&b=222&b=333 will become an array in the JSON, if you repeat the same query name with different values:
-- Going to break the filter functionality into its own function, instead of handling the filter functionality inside the <.get()> callback. -- This function will take in <req.query> as an argument & filter through the animals accordingly, returning the new filtered array.
-- 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.
*Add the following code:
*Test the route again in the browser: -with personalityTraits <hungry> and <zany>.
Enter the following command into the terminal:
You can specify the name of your application by adding <app-name> after <heroku create>. -- It must be a UNIQUE name that isn't used anywhere else on Heroku. -- If this is your first time using CLI, you'll be prompted to login with your Heroku account.
Should see output like the following image:
The deployment steps rely on a Git branch (just as with GitHub Pages).
We will push to <heroku main>: -- This is a new location for our repo's code to go. -- NOT pushing to <origin main>. -- The "remote" => <origin> or <heroku> in our <git push> commands.
i.e. We cannot say <git push heroku main> when we aren't currently in the local <main> branch.
We can push from a local feature branch to Heroku. -- To see our progress as we go & zoo will ultimately host the final product somewhere else.
DONE: Created app with Heroku!
-- 2 items in the output from the command line that look like links:
-- Tell our app to use that port if it has been set, and if not, default to port 80.
-- Go down to where we have our application listening>
-- 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!
SUCCESS!
Should see a JSON response when you navigate to:
(https://
View video about the difference between development (local) and production (Heroku) environments.
User Stories
As a user, I can request for a list of all animal data
As a user, I can request data for just one animal based on its id value
As a user, I want to be able to do this from anywhere and not just my computer