Please do the following to have the API working on your computer.
- Install yarn via this link if it is not yet done
- Open the navigator and browse to the project folder
- Install all the required dependencies with the command below
yarn install
- Make a copy of the
.env.example
to.env
cp .env.example .env
- Start the project with
yarn dev
Please check the terminal for the URL of the server. By default, it will be http://localhost:3000
- To run the tests, run
yarn test
The API's feature is to convert a temperature value in Celsius as input and gives the equivalent temperature in Farenheit as the output or convert a value in Celsius as input to Fahrenheit as output using the same endpoint.
We have the following query parameters:
value
: the value to convert, required, must be a numberfrom
: the unit to convert form, it can becelsius
orfahrenheit
, requiredto
: the unit to convert to, it can becelsius
orfahrenheit
, required, must be different fromfrom
decimal
: the number of decimal places to be returned, optional, must be a positive integer
A sample request to the API would look like https://myserver/api/convert-temperature?value=100&from=celsius&to=fahrenheit
The client is required to put the Content-Type
header as application/json
in the request.
The available status codes are as follows:
200
: Correct parameters are sent, the request was successful422
: Incorrect parameters are sent, the request was not successful, some parameters could not pass the validation500
: Internal server error, the server could not process the request or an error was encountered while processing the request404
: The requested resource could not be found429
: Too many requests, the server could not process the request
The response will be in the following format:
{
"from": "Celsius",
"to": "Fahrenheit",
"decimal": 0,
"value": 12,
"result": 54,
"resultRaw": "54°F"
}
A 422 response format will be as follows:
{
"type": "error",
"status": 422,
"message": "Invalid input",
"errors": [
{
"value": "p",
"msg": "value must be a number",
"param": "value",
"location": "query"
}
]
}
A 404 response format will be as follows:
{
"type": "error",
"status": 404,
"message": "The requested resource could not be found"
}
A 500 response format will be as follows:
{
"type": "error",
"status": 500,
"message": "Internal server error"
}
To have an idea of the activities of the API, all access to the API as all the errors that occur will be logged as well.
Logs will be stored in a log-YYYY-MM-DD.log
file in the logs
directory. A log file will contain all activity of the related day.
I implemented a rate limiting mechanism to prevent the API from being flooded with requests. This will also help prevent DDOS attacks.
The rate limit is set to 60
requests per minute for each IP address.
For testing, I wrote a few unit and feature tests for the API. The unit tests are written in tests/units.spec.js
and the feature tests are written in tests/feature.spec.js
. I used jest
and supertest
for the tests.
It is worth mentioning that the test suite is not exhaustive.
I decided to deploy the API on AWS Lambda and take advantage of the serverless architecture. Below are some of the reasons for that:
- It is easy to deploy and I can run my code without having to manage the infrastructure, AWS handles all the infrastructure
- It will scale automatically as the number of requests increase and decrease
- It is cost effective, only the compute time (by per-millisecond) will be charged
- It has a great integration with Node.js
- AWS offers lots of features to monitor the logs and to monitor the performance of the API
- Create an account on AWS and sign in
- Go to
Security Credentials
- Create
Access Keys
or use the existing ones if you have. If you have created new ones, keep a copy of theAccess Key ID
andSecret Access Key
- In your local machine, install globally
serverless
package usingnpm install serverless -g
- Configure serverless credentials with the
Access Key ID
andSecret Access Key
you created in step 3 by running
serverless config credentials --provider aws --key <Access Key ID> --secret <Secret Access Key>
- In
serverless.yml
in the API project folder, update the parameters accordingly - From your terminal in the API project folder, deploy the API to AWS Lambda using the following command:
yarn deploy
You can see the newly created endpoint in the console after deployment.
Happy dev!!!