generated from comp426-2022-spring/a03
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
41 lines (31 loc) · 1.06 KB
/
server.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
import { coinFlip, coinFlips, countFlips, flipACoin } from "./coin.mjs";
import express from "express";
import minimist from "minimist";
const app = express();
const args = minimist(process.argv.slice(2));
const HTTP_PORT = args["port"] || 5000;
//start an app server
const server = app.listen(HTTP_PORT, () => {
console.log("App listening on port %PORT%".replace("%PORT%", HTTP_PORT));
});
//default respnse for any other request
app.get("/app/", (req, res) => {
res.status(200).json("200 OK");
});
app.get("/app/flip", (req, res, next) => {
res.status(200).json({ flip: coinFlip() });
});
app.get("/app/flips/:number", (req, res, next) => {
const flips = coinFlips(req.params.number);
const summary = countFlips(flips);
res.status(200).json({ raw: flips, summary: summary });
});
app.get("/app/flip/call/heads", (req, res, next) => {
res.status(200).json(flipACoin("heads"));
});
app.get("/app/flip/call/tails", (req, res, next) => {
res.status(200).json(flipACoin("tails"));
});
app.use(function (req, res) {
res.status(404).send("404 NOT FOUND");
});