-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
86 lines (76 loc) · 2.38 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var express = require('express');
var app = express();
var os = require("os");
var babyNames = require('./babyNames')();
var port = 80;
app.get('/', function (req, res) {
let html = `
<!DOCTYPE html>
<html>
<head>
<title>Baby Names</title>
</head>
<body>
<h1>Baby Names</h1>
<form action="/girls" method="get">
<label for="rank">Rank:</label><br>
<select id="rank" name="rank">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="all">All</option>
</select><br>
<input type="submit" value="Get Top Girl Names">
</form>
<form action="/boys" method="get">
<label for="rank">Rank:</label><br>
<select id="rank" name="rank">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="all">All</option>
</select><br>
<input type="submit" value="Get Top Boy Names">
</form>
</body>
</html>
`;
res.send(html);
});
app.get('/girls', function (req, res) {
var rank = req.query['rank'];
let girlNames = babyNames.getGirlName(rank);
// Generate the HTML list items
res.send(generateHTML("Girls", girlNames)); // Send the HTML
});
app.get('/boys', function (req, res) {
var rank = req.query['rank'];
let names = babyNames.getBoyName(rank);
res.send(generateHTML("Boys", names));
});
app.listen(port, function (err) {
console.log('running server on port ' + port);
});
function generateHTML(Title, Names) {
let listItems = Names.map(name => `<li>${name}</li>`).join('');
// Generate the HTML
let html = `
<!DOCTYPE html>
<html>
<head>
<title>${Title}</title>
</head>
<body>
<h1>Names</h1>
<ul>
${listItems}
</ul>
</body>
</html>
`;
return html;
}