kitchen-boy / portfolio-generator

Module 9 - Node.js
1 stars 0 forks source link

Title: Generate entire website #5

Closed kitchen-boy closed 2 years ago

kitchen-boy commented 2 years ago

Description

kitchen-boy commented 2 years ago

9.5: Handle Node.js Asynchronicity

GOAL: final portfolio website, complete with a style sheet that looks like the following image:

Final portfolio website with style sheet

STEPS:

  1. Finalize output with a style sheet:
    • Download a style sheet & use the <**fs***> module to copy the file when we generate the HTML code to create a finished directory for the portfolio.
  2. Write a file using Promises:
    • We want to remove the use of callback functions & clean up the code. Let's create our own PROMISES to use instead.
  3. Copy a file using Promises:
    • Because we know how to create our own Promises, let's apply that knowledge to copy a file as well!
  4. Export the functions and wrap up:
    • Export our 2 functions for handling the project's output from the generate-site.js file & import them into app.js to include in the Promise chain.
  5. Save your progress with Git:
    • Merge our feature branch & close the GitHub issue to finish up our application.
kitchen-boy commented 2 years ago

9.5.3 Finalize Output with a Style Sheet:

Copy the Style Sheet:

Where do we want to include this functionality?

Refactor the current <fs> functionality to use Promises instead of callback functions in <app.js>

kitchen-boy commented 2 years ago

9.5.4 Write a File Using Promises:

Callbacks and Promises:

Used callback functions as follows:

Used Promises as follows:

2 Examples of Asynchronous code:

(callback function vs a Promise-based function): Asynchronous code - callback function vs Promises

Lesson Goal: Learn how to set up this type of functionality.

Create the First Promise:

Note: To use the <fs library in a file, import it using the Node.js <require()> function.

Will create 2 functions that return a Promise:

  1. one function for writing a file (i.e. the HTML file).
  2. another function for copying a file (i.e. the CSS style sheet). -- That means we'll execute an asynchronous function that enables us to use <.then()> & <.catch()> methods to handle its response.
    • Create the function write files ==> take the <fs.writeFiles()> function & contextualizing it to be Promise based. -- First, create the Promise. -- Then, work in the <fs> functionality we're looking to achieve.
    • Create the following function, the first JavaScript Promise, in <generate-site.js>: First Javascript Promise!

To create a Promise:

IMPORTANT Feel free to execute this code in generate-site.js. Just know that it will look for a dist folder inside the utils folder, and may throw an error if it doesn't find one. It's a good way to test the .catch() functionality, though!

Summary: Created first JavaScript Promise!

NOW: We have the functionality for creating the HTML file handled as a Promise.

kitchen-boy commented 2 years ago

9.5.5. Copy a File Using Promises:

  1. Make <copyFile()> return a new Promise object, like we do with <writeFile()> above. copyFile() return new Promise object

  2. Move the <fs.copyFile()> code form <app.js> into the returning Promise object's function into the <copyFile()**> function:

    • (use <writeFile()> as a guide: Move fs copyFile() code
  3. If there's an error, <reject()> the error and return out of the function: if error present, reject() the error   return out of function

  4. If it's successful, <resolve()> it with a success message: resolve() it with a success message

Done with writing this JavaScript Promise for <copyFile()> function!

kitchen-boy commented 2 years ago

9.5.6. Export the Functions and Wrap Up:

Simplify code, using object destructuring:

NOTE:

The Inquirer prompt Promise chain at the bottom of <app.js> should look as follows:

Inquirer prompt Promise chain at bottom of (app js)

Reiteration of this function's flow:

promptUser function

1. START: Ask user for their info with Inquirer prompts;

Inquirer prompts

this returns all of the data as an object in a Promise.

2. The <promptProject()> function captures the returning data from <promptUser()>.

promptProject function

3. The finished portfolio data object is returned

4. We pass <pageHTML> into the newly created <writeFile()> function which returns as a Promise.

5. Upon a successful file creation, we take the <writeFileResponse> object

6. The Promise returned by <copyFile()> then lets us know if the CSS file was copied correctly

SUMMARY: PROMISES

IMPORTANT:

kitchen-boy commented 2 years ago

9.5.7 Save Your Progress with GIt

  1. Commit and push your feature branch (feature/finished-site) to GitHub.

  2. Merge your local feature branch (feature/finished-site) into your local develop branch and push develop to GitHub.

  3. Close your GitHub issue regarding this feature.

How to deploy this application:

kitchen-boy commented 2 years ago

9.5.8. SUMMARY RECAP:

And to recap the entire module:

This is only the first step to creating a back end for the applications.

*In the next module, we'll learn about two very important concepts in web development:

  1. test-driven development (TDD) and
  2. -object-oriented programming (OOP)-.