Skip to content

LolloTech/openweathermap_backend

Repository files navigation

OpenWeatherMap_Backend

This is just an example of a webserver, that consumes OpenWeatherMap APIs for fetching some weather data.

Starting the web server application

Run

npm run start

from the root folder of the project. Alternatively, you can run it with Docker, using the Dockerfile in the project.

Mock up credentials with sql lite file

Please note: these credentials are provided in the sql lite used for mocking the communication with a database. I thought about mock a database with sql lite, by passing the need of a real dbms system. For our purpose of testing some rest APIs, for now it is ok to do so. These credentials could be user for testing the various routes made available by Express in ExpressServer.js through RESTful API calls.

username: admin, password: administrator username: apitest, password: api

What is this software

This is a small software that implements a hypothetical HTTPS backend server (with a self-signed certificate generated by me, to make possible to enable HTTPS connection). The aim of this micro project was to trying to implement a node server in the most simple way possible using Express.js and making one of the routes and middlewares totally async, including handling exceptions at the level of an exception that occurred in an async method.

Postman collection

I have included a collection containing all the possible implemented calls, importable in Postman, in the project. This should make it easy for you to use the server's APIs.

Technical considerations

Theoretical login and password change features have been implemented.

The password change requires that a login operation has been performed first, which returns a JWT token. Therefore, you must pass this token in the body of this request (I put the token in the body for the sake of speed. For consistency, it would be better to insert the JWT in the HTTPS request header as a bearer token).

After passing this token, once you have passed in the body the username, the old password, the new password (and of course, the token mentioned above), it is possible to change the password.

The acceptance of the JWT token can take place in two ways, at runtime (without saving settings at the database level): without checks or with a check at the token issuance date level. To activate JWT date limit validation, there is a dedicated route (setJWTDateLimitValidation).

I tried to make the code as decoupled as possible between the various parts, in order to be able to perform as many unit tests as possible (I focused on service classes, and very little on infrastructure classes).

For infrastructure classes (such as ExpressServer.js), I tried to perform as many integration tests as possible to cover all possible behaviors of all available routes.

Tests execution

npm run test: runs both integration and unit tests. npm run test-unit: runs only unit tests. npm run test-integration: runs only integration tests.

UserRepository, migrations, seeds (not necessary for this project, but I have included them for completeness)

Creating the sqlite database from scratch

Go to the root folder of the project in the terminal and then run:

npx knex --knexfile ./db/knexfile.cjs migrate:latest

a local file-based database, namely "db.sql3", will be generated.

Inserting default data into the sqlite database

Go to the project's root folder with the terminal and then execute:

npx knex  --knexfile ./db/knexfile.cjs seed:run

UserRepository drop

Go to the project's root folder with the terminal and then execute:

npx knex  --knexfile ./db/knexfile.cjs migrate:rollback

Docker

Image build from Dockerfile

Execute:

docker build --tag node-docker-openweathermap-backend .

Image RUN from docker file

Execute:

docker run -d -p 3003:3003 node-docker-openweathermap-backend

About

An example of implementation of some APIs exposed by OpenWeatherMap (https://openweathermap.org/)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published