The api of covics-19
This projects is the backend of the covics-19 project and has the purpose of providing the api for the frontend.
The backend consists in a NodeJS server, written in Typescript. It provides the api to the frontend in order to let it connect to the mondodb. The mongodb is hosted on mongoatlas, while the server is hosted on Heroku.
NodeJS is an interpreter for the Javascript programming language, it works similar to the python interpreter. Before NodeJS only browsers could interpret Javascript, that was used only in the frontend of websites. NodeJS has allowed Javascript to become a general-purpose programming language and it is now used expecially for servers.
Typescript is an extentension of Javascript that introduces types and interfaces in Javascript. f is a super-set of Javascript, this means that (more or less) every Javascript program could be also a Typescript program. It is a must for stable and elegant source code. It has to be transpiled in Javascript in order to be executed. Its compiler is installed as a global npm package (NodeJS package).
MongoDB is a non-relational database. Being it based on json objects, it works very well together with NodeJS. Its biggest advantages is that it has not any fixed schema, so it is very flexible. To install mongodb on Ubuntu, I suggest following this. Note that it also explains how to make mongod a linux service, that is always running. As any database, MongoDB is composed by a client (mongo) and a server (mongod). I also suggest installing MongoDB Compass to easily visualize a mongodb database.
To host the server I used Heroku, that comes with a free plan. It is synced to Github, every time the code (actually the dist folder) is updated on Github, Heroku notices this and updates the server. In particular the start script inside the package.json file is executed. From the online Heroku dashboard, it is possible to configure the project, for instance set the env vars such as the Mongo Atlas uri or view the current log.
To host the databas I used Mongo Atlas, an on-the-cloud MongoDB instance that also supports a free plan. There have been created three users (admin, covics-19 and covics-19-readonly). Clicking on connect will provide you the uri to connect to the database from a terminal, mongo compass or your program. The network access has also been setted to everyone, so that you can make requests to the database from wherever you want.
To start developing the backend:
- Install NodeJS
- Install MongoDB
- Clone this repo by executing
git clone https://github.com/covics-team-19/backend.git
- On the root folder, install the NodeJS dependencies executing
npm i
. It will install also the Typescript compiler. - To transpile Typescript into Javascript, execute
npm run transpile
- To execute the transpiled Javascript, execute
npm start
. The server should be listening on port 3000 - To run both in an only command, execute
npm run serve
When running in local, a mongodb server (mongodb) should be running and listening (default uri is mongodb://localhost:27017
).
To deploy and update the server, just transpile the code (npm run transpile
) and push on Github.