-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
40 lines (36 loc) · 989 Bytes
/
server.js
File metadata and controls
40 lines (36 loc) · 989 Bytes
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
const express = require('express');
const app = express();
app.get('/', (request, response) => {
response.send('hello! i work');
});
app.get('/flip-coin', (request, response) => {
const randomNumber = Math.random();
let coinValue = '';
if (randomNumber < 0.5) {
coinValue = 'Heads!';
} else {
coinValue = 'Tails!';
}
response.send(coinValue);
});
app.get('/flip-coins', (request, response) => {
const times = request.query.times;
if (times && times > 0) {
let heads = 0;
let tails = 0;
for (let i = 0; i < times; i++) {
const randomNumber = Math.random();
if (randomNumber < 0.5) {
heads++;
} else {
tails++;
}
}
response.json({ heads, tails });
} else {
response.send('hey! you need to send times!');
}
});
app.listen(5010, () => {
console.log('started server. listening on port 5010.');
});