localhost:3000/seeds/show/33 is the parameter. This parameter can be read by the main.js script and can determine the output displayed the browser window. You define a parameter using the app.get method, for example:
app.get("/seeds/show/:n", (req, res) => {
// body of anonymous function goes here
});
The value of the parameter is obtained by main.js like this:var n = req.params.n;
localhost:3000/person&name=Alice&gender=F&age=11In main.js, to obtain the query as an object literal:
var q = req.query;The resulting value of q is
{ name: 'Alice', gender: 'F', age: '11' }
-----------------------------------------
// Source code file: main.js
const express = require("express");
const fs = require("fs");
const app = express( );
app.set("view engine", "ejs");
app.get("/student_info", (req, res) => {
res.render("student_info");
});
app.get("/write_info", (req, res) => {
var output = JSON.stringify(req.query);
fs.writeFile("data/student_info.json",
output, (err) => {
if (err) {
res.send("Error writing to output file.");
}
else {
res.send("Student info " +
"written to JSON file.");
}
});
});
app.listen(3000, ( ) => {
console.log("Server started.");
});
-----------------------------------------
<!DOCTYPE html>
<!-- Source code file: student_info.ejs -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Student Information</title>
</head>
<body>
<h1>Student Information</h1>
<form name="form1"
action="/write_info"
method="get">
<label for="fullName">Full Name:</label><br>
<input type="text" name="fullName"><br><br>
<label for="year">Year:</label><br>
<input type="text" name="year"><br><br>
<label for="gpa">GPA:</label><br>
<input type="text" name="gpa"><br><br>
<input type="submit" value="Submit Student Info">
</form>
</body>
</html>
-----------------------------------------
var pets =
{
ID1001: { name: 'Sadie', animalType: 'dog', age: 3 },
ID1009: { name: 'Coco', animalType: 'cat', age: 4 },
ID1013: { name: 'Slinkie', animalType: 'snake', age: 2 },
ID1016: { name: 'Thumper', animalType: 'rabbit', age: 2 },
ID1027: { name: 'Max', animalType: 'dog', age: 5 },
ID1029: { name: 'Duke', animalType: 'dog', age: 7 },
};
How can you determine if an object contains a specified key, for example
ID1016?
-------------------------------------------------
// Display a name obtained from an input form with
// method=GET
// Source code file: main.js
const express = require("express");
const app = express( );
app.use(express.urlencoded({extended: false}));
app.set("view engine", "ejs");
app.get("/contact_info", (q, r) => {
r.render("contact-info");
});
app.post("/contact_receipt", (q, r) => {
console.log(q.body);
r.render("contact-receipt", q.body);
});
app.listen(3000, ( ) => {
console.log("Listening on port 3000.");
});
-------------------------------------------------
// Source code file:
<!DOCTYPE html>
<!-- Source code file: contact-info.ejs -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TestEjs2 Example</title>
</head>
<body>
<h2>TestEjs5 Example</h2>
<form name="form1"
action="/contact_receipt"
method="post">
<label for="fname">First Name:</label><br>
<input type="text" name="fname"><br><br>
<label for="lname">Last Name:</label><br>
<input type="text" name="lname"><br><br>
<label for="phone">Phone:</label><br>
<input type="text" name="phone"><br><br>
<label for="email">Email:</label><br>
<input type="text" name="email"><br><br>
<input type="submit" value="Submit Contact Info">
</form>
</body>
</html>
-------------------------------------------------
// Source code file:
<!DOCTYPE html>
<!-- Source code file: contact-receipt.ejs -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TestEjs5 Example</title>
</head>
<body>
<h1>TestEjs5 Example</h1>
<p>Your contact information has been received<br>
Thank you for your information.</p>
<table>
<tr>
<th>First Name</th> <th>Last Name</th>
<th>Phone</th> <th>Email</th>
</tr>
<tr>
<td><%= fname %></td> <td><%= lname %></td>
<td><%= phone %></td> <td><%= email %></td>
</tr>
</table>
</body>
</html>
------------------------------------------------