The Fibonacci REST API has only one single feature:
- Given an index
n, the API will reply with the value of thenposition of the Fibonacci series. Example: ifn=3response should be2, ifn=6response should be8.
This repository will be used to versioning the code of the solution but also as a journal or notebook about the process followed to implement the solution.
Pre-reqs:
- node.js
-
Clone this repo
git clone https://github.com/rastangineer/fibonacci-rest-api.git cd fibonacci-rest-api -
Install packages
npm install
-
start server locally
node app.js
-
Send requests with Postman or a similar
-
I don't consider myself a
REST APIdeveloper, or even a developer.- But I have plenty of experience working with middleware, HTTP, SOAP and REST protocols. I used to be a Java developer long time ago.
-
What programming language to use?
- From previous personal projects I can say I'm more familiar with
node.jsand I read thatexpress.jsis a web framework for node.js that allows you create to REST APIs quickly. So the winner is node.js + express.js
- From previous personal projects I can say I'm more familiar with
-
I'm familiar with
REST APIservices,SOAP/HTMLWeb Services and similar technologies, but should I start with design or just start googling and try?LoadingsequenceDiagram participant C as :Client participant S as :Server C ->>+ S: GET /api/fibonacci_number (index) S ->>+ S: Generate Fibonacci series S -->>+ C: Response with fibonacci[index] value
-
How do I start with something basic?
- I like to start with the very basics, and then gradually add funcionality in a iterative and incremental way. Since I'm new developing REST APIs I would start with the most basic thing, the famous Hello World, for example an API that just replies with the text
Hello World. - Next step would be passing something like the
usernameusing parameters and reply with something likeHello World username. - Now that we are able to pass and read parameters, we can start thinking about how to generate the Fibonacci series, from what I remember from college this is a very good use case for recursive programming.
- I like to start with the very basics, and then gradually add funcionality in a iterative and incremental way. Since I'm new developing REST APIs I would start with the most basic thing, the famous Hello World, for example an API that just replies with the text
-
Thought process?
-
Technical decisions?
- What is the maximun index
nallowed.- In theory, it shouldn't have a limit. So it's important to be able to handle big values for
n.
- In theory, it shouldn't have a limit. So it's important to be able to handle big values for
- Harcoded Fibonacci series? Or Fibonacci series generated on each request.
- What is the maximun index
-
Optimizations
- After initial tests with
recursive programmingI have found that this a very time consuming method, the bigger the parameter the longer it takes to generate the fibonacci series. - Alternative 1:
- Using the golden ratio
phi ^ n / sqrt(5)wherephi = (1+ sqrt(5)) / 2we might get better performance, but values might not be accurate aftern > 77. Reference
- Using the golden ratio
- Alternative 2:
- Using a while loop, by using the formula
f(n) = f(n-1) + f(n-2)wheref(0) = 0andf(1) = 1
- Using a while loop, by using the formula
- Alternative 3:
- How about hard-coding the fibonacci series and store it in an array, then just reply with
array[n], no calculations. Just saying I haven't really thought about it.
- How about hard-coding the fibonacci series and store it in an array, then just reply with
- After initial tests with

