A small REST API of Timezones.
This server is created with:
- NodeJS: 14.15.3
- ExpressJS: 4.17
- Cors: 2.8.5
- MongoDB: 3.6.3
- Axios: 0.21.1
- DotEnv: 8.2.0
- Morgan: 1.10.0
Follow the next steps to run the server in your local enviroment:
You need to have installed the NodeJS Version 14.15.3. You can easily install it with nvm.
nvm install 14.15.3
Then, you need to set as the active version of node:
nvm use 14.15.3
git clone https://github.com/Cabezaa/timezones-server.git
cd timezones-server
npm install
It is not recommended to have the .env file committed in the repository. In this case, it was done in that way just to make it easier to clone and test the server.
Information in the ENV:
PORT ==> The port where the server will run. 3005 as default.
DB_URL ==> MongoDB URL. Localhost as default.
DB_PORT ==> MongoDB Port. 27017 as default.
DB_NAME ==> The name of the DB. 'timezones' as default.
WORLDTIMEAPI_URL ==> The World Time API URL to fetch the data. 'http://worldtimeapi.org/api' as default.
ALLOWED_ORIGINS ==> The URL of the Client APP to set CORS Options. 'http://localhost:3000' as default.
MONGO_CONTAINER_NAME ==> The name of the mongo container for connect in 'production'. 'mongo' as default.
The project has installed Nodemon for dev environment. You can run the server with nodemom with:
npm start
The project has a few test cases for the API endpoints. They are located in the test/
folder.
The test cases was made with Mocha and Chai. Also i used Chai-http to test the API.
To run the test run the next command:
npm run test
It will call Mocha
with a 30000ms of timeout.
Important: The test suite will create a new database in the same connection, but with another name (timezones_test
).
The application is ready to run in docker containers. In this case, we need a container for the node server and another for the mongodb. So i created a docker-compose file to create and run both:
docker-compose build
docker-compose up -d
docker-compose down
Important: To make the nodejs container wait until the mongodb container is up and ready i used a sh script called wait-for-it.sh.
In the case that you only want to build and run the server container (and use a external or preexisting mongodb system) follow the next steps:
docker build -t timezones-server:latest .
docker run --name timezones-server -p 3005:3005 -d timezones-server:latest
In this section, i will describe the available endpoints:
GET /timezones/
To get the full list of Timezones available. It will hit the WorldTime API the 1st. time (in other case, will use local cache)
No parameters
Status 200: An array of timezones objects:
[{
"_id": "string",
"name": "string",
"show": boolean,
"date": "string",
"time": "string"
}]
Status 504: A error object. This happens when the WorldTime API is down or busy.
GET /timezones/:name
It will return a specific timezone with his date and time in string format.
* name - "string" - the name of the timezone to get
Status 200: A timezone object with date and time:
{
"_id": "string",
"name": "string",
"show": boolean,
"date": "string",
"time": "string"
}
PUT /timezones/:name
It will update the show
attribute of a timezone and set it in true
.
* name - "string" - the name of the timezone to update the 'show' property to `true`
Status 200: A timezone object:
{
"_id": "string",
"name": "string",
"show": boolean,
}
DELETE /timezones/:name
It will work as a "Soft Delete", because it will update the show
attribute of a timezone and set it in false
.
* name - "string" - the name of the timezone to update the 'show' property to `false`
Status 200: A timezone object:
{
"_id": "string",
"name": "string",
"show": boolean,
}