aboutsummaryrefslogtreecommitdiff
path: root/entries/braxtonhall/express/index.js
blob: 117a04b30949f079d589a4a34ed5c3dab2eff424 (plain) (blame)
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
import axios from "axios";
import express from "express";

const PORT = 3000;

const app = express();

const {get} = axios.create({baseURL: `http://localhost:${PORT}`});

const getFib = (n) => get("/fib", {params: {n}}).then(({data}) => data);

app.get("/fib", async (req, res, next) => {
	const n = Number(req.query.n);

	if (!isFinite(n) || n < 0) {
		res.status(400).json("`n` must be a natural number");
	} else if (n <= 1) {
		res.status(200).json(n);
	} else {
		const [nSub1, nSub2] = await Promise.all([getFib(n - 1), getFib(n - 2)]);
		res.status(200).json(nSub1 + nSub2);
	}
	return next();
});

app.listen(PORT);