Server for Weather Vortex Project.
Table of contents
Generate your .env
file following the example in .env.example
. You will be required to generate api keys for Open Weather Api and for Troposphere.
! If you haven't some keys, the application will continue to run, but forecasts for missing services will not be given when requested.
In local environment, you don't need anything to build this project, since it's all in Javascript. Just run the following command in route folder to install all dependencies:
npm install
To build Docker image, run the following command:
docker build . -t <your username>/weather-vortex-server
You can see your container image with docker images
.
In your local environment, you can run with npm run start:local
command. You need a local mongodb installation to use Users feature at the moment. You can use a mongodb container too.
To run your container, use the following command:
docker run -p 12000:12000 -d <your username>/weather-vortex-server
Since this is a detached container, to see all logs run
docker ps
docker logs <container id>
The future is now! Check the docker compose repository. Reproduce an entire working environment with server, client, database and iot weather station too!
In your local environment, you can test with npm test
command. You can run a code coverage check wih npm run test-cov
command. Produce an html report with npm run test-cov:html
. You can run a single test suite with the command npx mocha -r dotenv/config --trace-warnings <path-to>/*.test.js --exit
.
Navigate in your browser to localhost:12000
to see the server ok page, or in your shell using those commands:
$ curl -i localhost:49161
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 15
ETag: W/"f-51rzdr5zfwWQpf+iUepheZg7siQ"
Date: Mon, 09 Aug 2021 20:38:01 GMT
Connection: keep-alive
Keep-Alive: timeout=5
{"result":"ok"}
In local environment, you should have a working mongodb installation or use a container:
docker run -p 27017:27017 -d mongo
docker ps
CONTAINER ID IMAGE CREATED STATUS PORTS
2fffd3d83939 mongo 4 seconds ago Up 3 seconds 0.0.0.0:27017->27017/tcp, :::27017->27017/tcp
64bbb7ec15d7 daniele.tentoni2/weather-vortex-server 4 minutes ago Up 4 minutes 15600/tcp, 0.0.0.0:49161->12000/tcp, :::49161->12000/tcp
You can start a local mongo container with persistent volume:
sudo docker run -d -v /path/to/data/:/data/db --name mongo -p 27017:27017 mongo
If you run into this test failure message:
GET forecasts for Cesena
1) "before each" hook for "responds with unsuccessful result"
...
1) GET forecasts for Cesena
"before each" hook for "responds with unsuccessful result":
Error: Timeout of 3000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/Users/d.tentoni/Documents/uni/weather-vortex-server/test/forecasts.test.js)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7)
It's very likely you don't have configured mongodb as needed. Read some rows before this paragraph how to configure Mongodb in your local environment or use it in a container.
As an additional check, look for a Database connected
string in the server console output. If there isn't, check your database connection.
The structure of the project is something like this:
-> /src
|
+-> /controllers: where we respond to user requests
|
+-> /models: where we define our data models
|
+-> /routes: where we define out rest api routes for user requests
|
+-> /storages: where we define all operations to do on our data
|
+-> /index.js: where we put all together
-> /test: where we define unit and functional tests
You can read a generic request flow as:
-> index.js -> route -> controller -> storage -> model -+
|
user <- controller storage <-+
In the controller, the instruction res.status().json({})
send the response to the user.