Goal: Create an Express website that displays vegetable seed for sale at the Urban Farmer company.
"scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "nodemon" },
[{"vegetable":"Carrot", "variety":"Nantes", "organic":"Yes", "numSeeds":1000, "sciName":"Dauens Carota", "price":"3.00", "image":"carrot.jpg"}, {"vegetable":"Brussels Sprouts", "variety":"Long Island", "organic":"No", "numSeeds":"200", "sciName":"Brassica oleracea", "price":"3.00", "image":"brussels_sprouts.jpg"}, {"vegetable":"Broccoli", "variety":"Green Magic", "organic":"No", "numSeeds":"100", "sciName":"Brassica oleracea", "price":"4.00", "image":"broccoli.jpg"}, {"vegetable":"Cucumber", "variety":"Double Yield", "organic":"Yes", "numSeeds":"300", "sciName":"Cucumis sativis", "price":"4.00", "image":"cucumber.jpg"}]
const app = express( );
app.set("view engine", "ejs");
var seeds;
<table> <tr> <th>Vegetable</th> <th>Variety</th> <th>Number of Seeds</th> <th>Price</th> </tr> <% var n = 0; for(var s of seeds) { %> <tr> <td><%= s.vegetable %></td> <!-- Add <td> tags to also display the variety, number of seeds, and price. Add the following td tag to display a link to the show.ejs view for each row. --> <td><a href="/seeds/show/<%= n %>">Show</a></td> </tr> <% n++; } %> </table>The EJS files should have software headers that look like this:
<!DOCTYPE html> <!-- Steve Robinson Tutorial 5 Nov 12, 2023 -->
res.render("index", {seeds: seeds});
localhost:3000/seedsThe browser should show an index page similar to: index.ejs
app.use(express.static("public"));Link a stylesheet to the index.ejs page. You can use this stylesheet for the index, show, and new views:
table { border-collapse: collapse; } td { border: 2px solid #004040; } td, th { padding: 8px; } img { width: 200px; }Also add body styles for background-color, color, and font-family. The stylesheet should have a software header that looks like this:
/* Steve Robinson Tutorial 5 Nov 12, 2023 */
<h1>Tutorial 5: Show View</h1> <p>ID: <%= n =><br> Vegetable: <%= seeds[n].vegetable %><br> Variety: <%= seeds[n].variety %><br> <!-- Add lines to display data for Organic (key=organic), Number of Seeds (key=numSeeds), Scientific Name (key=sciName), Price (key=price) --> </p>
app.get("/seeds/show/:n", callback);The callback function obtains the value of the parameter like this:
var n = req.params.n;It should also check that n is between 0 and elements.length - 1, inclusive. If it is, render the show.ejs view, passing the data object { seeds: seeds, n: n} to the view:
res.render("/seeds/show", { seeds: seeds, n: n });The show.ejs view should display the fields of nth item in the seeds array like this: show.ejs. If n is not an item in the array, use res.show to display the string "<p>Vegetable not found.</p>" in the browser. Wait until Item 14 to display the vegetable image. Here is the source code for the /seeds/show route in main.js:
app.get("/seeds/show/:n", (req, res) => { var n = req.params.n; if (0 <= n && n < seeds.length) { res.render("show", { seeds: seeds, n: n }); } else { res.send("<p>Vegetable not found.</p>"); } });
localhost:3000/seeds/show/4You should see a page similar to this: show.ejs
<img src="/images/<%= s.image %>">
<form name="form1" action="/seeds/append" method="get"> <label for="vegetable">Vegetable:</label><br> <input id="vegetable" name="vegetable"><br><br> <label for="variety">Variety:</label><br> <input id="variety" name="variety"><br><br> <label for="organic">Organic:</label><br> <input id="organic" name="organic"><br><br> <label for="numSeeds">Number of Seeds:</label><br> <input id="numSeeds" name="numSeeds"><br><br> <label for="sciName">Scientific Name:</label><br> <input id="sciName" name="sciName"><br><br> <label for="price">Price:</label><br> <input id="price" name="price"><br><br> <label for="image">Image:</label><br> <input id="image" name="image"><br><br> <input type="submit" value="Submit Data"> </form>
app.get("/seeds/append", (req, res) => { var newRow = req.query; seeds.push(newRow); res.render("index", { seeds: seeds }); });
{vegetable:"Pumpkin", variety: "Cinderella", organic: "No", numSeeds: 10; sciName: "Cucurbita pepo", price: 4.00, image: "pumpkin.jpg"}
---- index.ejs --------------------------------- <p><a href="/seeds/new">Add New Table Row</a></p> ---- show.ejs ---------------------------------- <p><a href="/seeds">Back to Index> <a href="/seeds/new">Add New Table Row</a></p> ---- new.ejs ----------------------------------- <p><a href="/seeds">Back to Index></a></p>
Grading Breakdown: Functionality: 35; Source code comments: 5; Source code headers in main.js and EJS views: 5; Submitted: 5 (project folder name should be tutorial5-<Your Last Name>