This document describes how to set up and use the REST Web Service. The service retrieves population data for Luxembourg from a Statec API and returns it in JSON format.
Ensure you have the following installed:
- Node.js (v18 or higher)
- npm (Node Package Manager)
Clone the repository and navigate to the project directory. Then, install the required dependencies:
npm installRun the following command to start the server:
npm startBy default, the server runs on port 3000.
The base URL of the service is:
http://localhost:3000
Endpoint:
GET /population/:year
Description:
Fetches the total population, male population (Luxembourgish and foreign), and female population (Luxembourgish and foreign) for the specified year.
Parameters:
:year(path parameter): The year for which the population data is requested.
Example Request:
GET http://localhost:3000/population/2020
Example Response:
{
"totalPopulation": 634000,
"malePopulation": {
"total": 317000,
"luxembourgish": 200000,
"foreign": 117000
},
"femalePopulation": {
"total": 317000,
"luxembourgish": 200000,
"foreign": 117000
}
}If the requested year does not exist in the dataset, the service returns population data for the nearest years (before and after).
Example Request:
GET http://localhost:3000/population/1825
Example Response:
{
"error": "No data found for year 1825. Closest year before: 1821. Closest year after: 1839.",
"closestYears": {
"closestYearBefore": 1821,
"closestYearAfter": 1839
}
}If the input year is invalid (e.g., not a number), the service responds with a 400 Bad Request.
Example Response:
{
"error": "Invalid year"
}src/server.js: Main application file that handles routes and logic.src/populationService.js: Contains logic to fetch and process population data from the Statec API.
-
Start the server:
npm start
-
Use
curlor a browser to test the endpoint:curl http://localhost:3000/population/2020
-
View the JSON response with population data.
This document explains how to build and run the REST Web Service using Docker.
The Dockerfile is divided into two stages:
-
Build Stage:
- Installs dependencies and copies the source code.
- Prepares the application for production.
-
Production Stage:
- Creates a lightweight container with only the necessary files and dependencies.
- Sets the working directory and starts the application.
Ensure you have the following installed:
- Docker (v20.10 or higher)
Run the following command to build the Docker image:
docker build -t population-server .This command:
- Pulls the Node.js 18 Alpine base image.
- Copies the application code and dependencies into the container.
- Produces a minimal production-ready image.
Run the container using:
docker run -p 3000:3000 population-serverThis command:
- Maps port
3000of the container to port3000on your host machine. - Starts the server.
Once the container is running, you can access the service at:
http://localhost:3000
After running the container:
http://localhost:3000/population/<year>
Example:
http://localhost:3000/population/2020
To stop the container, find the container's ID:
docker psThen stop it using:
docker stop <container-id>This guide explains how to deploy the Population Server application on a local Kubernetes cluster using Minikube. The deployment will create two replicas of the application and expose it as a LoadBalancer service for access.
Example video: https://drive.google.com/file/d/1y57Wl_e5sOPEpgIaeaeV3WKgWyA0qXQU/view?usp=sharing
- Minikube installed and configured.
- kubectl command-line tool installed.
- Docker is installed and running.
- Launch Minikube:
minikube start
- Verify Minikube is running:
minikube status
Minikube uses its own Docker environment, so you need to build the image there:
-
Set up Docker to use Minikube’s environment:
& minikube -p minikube docker-env | Invoke-Expression
-
Build the Docker image:
docker build -t population-server:latest . -
Confirm the image is available in Minikube:
docker images
-
Use the provided
deployment.yamlfile to create the Deployment andservice.yamlfor Service:kubectl apply -f deployment.yaml kubectl apply -f service.yaml
-
Verify that the pods are running:
kubectl get pods
Example output:
NAME READY STATUS RESTARTS AGE population-server-<random-uid> 1/1 Running 0 2m population-server-<random-uid> 1/1 Running 0 2m -
Check the Deployment and Service status:
kubectl get deployments kubectl get services
-
Get the URL for the LoadBalancer service:
minikube service population-server --url
-
Open the returned URL in your browser or test it using
curl:curl <minikube-service-url>
When you’re done with the cluster, you can stop and delete Minikube to save resources:
-
Stop the Minikube cluster:
minikube stop
-
Delete the cluster:
minikube delete