-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathapp.js
42 lines (36 loc) · 1.04 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
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"
}
function getAnimal() {
return animal = _.sample(Object.entries(animals));
}
const app = express();
app.get('/', function(req, res){
const [animal_name, sound] = getAnimal();
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(`George Orwell had a farm.<br />
E-I-E-I-O<br />
And on his farm he had a ${ animal_name }.<br />
E-I-E-I-O<br />
With a ${ sound }-${ sound } here.<br />
And a ${ sound }-${ sound } there.<br />
Here a ${ sound }, there a ${ sound }.<br />
Everywhere a ${ sound }-${ sound }.<br />`);
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 }`)
});