forked from newrelic/newrelic-opentelemetry-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
35 lines (29 loc) · 737 Bytes
/
routes.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
// routes.js
const express = require("express");
const router = express.Router();
const fibonacci = (n) => {
if (n < 1 || n > 90) {
throw new Error("n must be 1 <= n <= 90");
}
const sequence = [1, 1];
if (n > 2) {
for (let i = 2; i < n; i++) {
sequence.push(sequence[i - 1] + sequence[i - 2]);
}
}
return sequence.slice(0, n);
};
router.get("/fibonacci/:n", (req, res) => {
const n = parseInt(req.params.n);
try {
const result = fibonacci(n);
res.json({ result });
} catch (error) {
res.status(400).json({ error: error.message });
}
});
// Catch-all route handler
router.use("*", (req, res) => {
res.status(404).json({ error: "Route not found!" });
});
module.exports = router;