Hamburger Icon
  • Labs icon Lab
  • Core Tech
Labs

Practice: Node.js and Express Foundations

This lab provides a hands-on environment to help you practice the concepts learned in the course "Node.js and Express Foundations."

Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 1h 11m
Published
Clock icon Aug 15, 2024

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Welcome

    Welcome to the lab:

    Practice: Node.js and Express Foundations

    This lab provides a hands-on environment to help you practice the concepts learned in the course Node.js and Express Foundations

    Feel free to reference the course if you have any questions regarding the concepts covered in this lab.

    Lab Differences Before you begin, there are a couple differences from the course that you will need to be aware of.
    1. First, the packages required for this application have already been installed, You can verify this by opening the package.json file and reviewing the "dependencies" section.

    2. The other difference pertains to the separation of the Express app configuration from the starting of the Express host. When you open the index.js file, you'll notice that it retrieves the app instance from the app.mjs file and then starts the Express host. This separation was introduced to facilitate both running and testing the Express application.

    In the next Step you will begin developing the Express application in the app.mjs file.

    info> Each of the following Steps contain several Tasks to accomplish. If you are stuck on any task, attempting to Validate the task 3 times will present you with the solution.

    To get started click the > Next step button.

  2. Challenge

    Configure a Simple Express Server

    The first step is to configure a simple express server in the app.mjs file.

    Opening the src/app.mjs file, you'll notice that the app variable is currently being initialized to an empty object.

    This will need to be changed to an Express application.

    To start the Express application you can execute the following command in the Terminal window.

    node --watch src/index.js
    

    Then switch to the Web Browser tab and click the Refresh circle button.

    info>The above command is using the --watch flag which should rebuild the application whenever you make any changes. To stop the application you can use the key combination ctrl+c

  3. Challenge

    Rendering Static Files in Requests

    The next step is to host static files that can be served directly to the browser. To help test that the static file configuration has been configured correctly, the file hello.html has been placed at the root of the public directory.

    If you have not yet started the Express application in --watch mode, you can now start the application by executing the following command in the Terminal.

    node --watch src/index.js
    

    Then in the Web Browser tab you can change the url in the address bar to localhost:3000/hello.html and refresh the page.

    You should now see Hello, Static File in the Web Browser.

  4. Challenge

    Writing and Loading EJS Templates

    Now that you have added support for static files, you can now use the concert templates in the src/public/views directory.

    This application is using the EJS templating engine. Your next step is to configure the EJS engine and add a concerts endpoint that will serve up the concerts.ejs template. To view the results of this new route, switch to the Web Browser tab and change the url in the address bar to point to the route localhost:3000/concerts

    Starting the Express Application As a reminder you can start the **Express** application using the command ``` node --watch src/index.js ```

    You may notice that the page is currently reporting that No Concerts Have Been Scheduled.

    Opening the file src/public/views/concerts.ejs and scrolling down to line 32, notice that this template is iterating over a concerts array. Your next task is to pass this concerts array to the template. Refreshing the Web Browser, you should now see a list of available concerts.

  5. Challenge

    Accessing Route Parameters

    Now that you have the /concerts page listing all available concerts, your next step is to create a route to display the details of each concert.

    If you click on the PURCHASE DETAILS button you should receive an error message stating Cannot GET /concerts/1. If you still have the Express application running, you can view this change by typing the following detail path in the Web Browser address bar.

    localhost:3000/concerts/1
    
  6. Challenge

    Error Handling Middleware

    The final step of this lab is to add a middleware error handler that will capture all unhandled application errors and respond to the user with a more friendly message. To test this error handler, you can simulate an error situation by modifying the root route to immediately throw an error. Such as:

    app.get('/', (request, response) => {
      throw new Error()
    })
    

    Then change the url in the Web Browser to the root path localhost:3000 and refresh the browser.

  7. Challenge

    Congratulations

    Congratulations on completing all of the steps of this practice lab!

    The Node.js and Express Foundations course covers several more concepts than those explored in this lab. Feel free to continue using this environment to explore other concepts that were addressed.

    Wishing you the best as you continue your Node.js learning journey and looking forward to seeing what you will create!

Jeff Hopper is a polyglot solution developer with over 20 years of experience across several business domains. He has enjoyed many of those years focusing on the .Net stack.

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.