-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
83 lines (72 loc) · 1.97 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
const express = require('express');
const _ = require('underscore');
var port = process.env.PORT || 8080;
var animals = {
"cat": "meow",
"dog": "bark",
"eel": "hiss",
"bear": "growl",
"frog": "croak",
"lion": "roar",
"cow": "moo"
}
function getAnimal() {
return animal = _.sample(Object.entries(animals));
}
const app = express();
app.get('/', function(req, res){
const [animal_name, sound] = getAnimal();
const htmlResponse = `
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: black;
color: white;
font-family: 'Patrick Hand SC', cursive;
text-align: center;
padding-top: 40px;
}
p {
font-size: 30px;
margin: 5px;
}
p:nth-child(odd) {
color: #00ff00; /* Green color for odd paragraphs */
}
p:nth-child(even) {
color: #ff0000; /* Red color for even paragraphs */
}
.title {
font-size: 48px;
font-weight: bold;
margin-bottom: 20px;
}
@import url('https://fonts.googleapis.com/css2?family=Patrick+Hand+SC&display=swap');
</style>
</head>
<body>
<div class="title">Old MacDonald had a farm.</div>
<p>E-I-E-I-O</p>
<div class="animal-sound">And on his farm he had a ${ animal_name }.</div>
<p>E-I-E-I-O</p>
<div class="animal-sound">With a ${ sound }-${ sound } here.</div>
<div class="animal-sound">And a ${ sound }-${ sound } there.</div>
<p>Here a ${ sound }, there a ${ sound }.</p>
<div class="animal-sound">Everywhere a ${ sound }-${ sound }.</div>
</body>
</html>
`;
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(htmlResponse);
res.end();
});
app.get('/api', function(req, res){
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(animals));
res.end();
})
module.exports = app.listen(port, () => {
console.log(`Launching server on http://localhost:${ port }`)
});