You can copy and paste the code in to VSCode to try these. You are welcome to use this code
in your assignment but please do give a reference to what you use.
Example 1: A simple multi-page products navigation and display.
Note that this example does not require a custom server (unlike your Assignment 3). Much of the code will be simpler and easier to do on the server. Just be aware that you cannot copy this code into your server. The ideas are the same but it will be implemented a bit differently.
The first thing is to organize up the data in an easy to use format. Here we have an object with properties (i.e. “keys”) are the product types that indicate a group of related products. Each product type key has a value that is an array of product objects that hold the information about a particular product. Instead of an array we could have used an object if we had unique names or id’s for the products. This would be helpful if we ever have to “look up” an particular product. With an array you would have to search though it. With an object you would need only see if the unique key for the product exists in the object.
Save each of the items below in a file named at the top:
save as products.json
This code will load the JSON file of products data from the server and make it available in a web page. You may recall that we couldn’t just load the file as a src to a script tag. We had to save the data as javascript code. This is a better way to handle sharing data between the client and server. You could also generate the JSON on the server rather than have it hand over the file.
save as functions.js
save as index.html
Below will display a product page based on the product_key in the query string. The product_key must match a key in products.json for the products to display (this is mostly to automatically create the links on the index.html page). There is also a navigation bar that automatically creates the page links from the products.json keys.
save as display_products.html
Example 2: A simple shopping cart example.
Because the quantities are always taken from the session (i.e. the ‘cart’), this is also an example of using a session to make a form sticky.
Save this as display_products.html
We add a server to handle the sessions to keep track of different users data when they go from page to page. We also use the server to provide “microservices” to give product info and session data.
Save the following as server.js
Example 3: Creating an invoice to both print and email (also example of node mailer component) example
Add the following to the server.js file from previous example. You may wish to add a “checkout” button or link to this route in the cart.html page.