Skip to content

girls-incode/microservices-mongo-csv-importer

Repository files navigation

Node Microservices + MongoDB + Docker Compose

Build two Express Microservices that share the same database and have two endpoints:

  1. POST: expects a CSV payload and saves the data to a MongoDB database
  2. GET: reads data from the same database and serves JSON responses. Add Filters on the data as query parameters.

Run the app inside docker

docker-compose up --build

It builds 4 images:

  • microservices-mongo-csv-importer_apifilters
  • microservices-mongo-csv-importer_dataimporter
  • mongo
  • node

and 3 containers:

  • apifilters
  • dataimporter
  • mongodb

The containers communicate with each other on a network named 'micro-app'.

Run each microservice separately

npm run start
npm run dev

Test API endpoints

  1. CSV Importer

With a tool like Postman, make a POST request at http://localhost:5000/ (it suppose the input file name is 'emissions')



To see the mongodb data:

docker exec -it mongodb bash
mongo
show dbs
use vizuality
show collections

# try some filters
db.emissions.find({country:"CZE"}).pretty()
db.emissions.find({parent_sector: {$ne: ""} }).pretty()
db.emissions.aggregate([{
	$match: {
        country: "ABW",
        sector: "Total including LULUCF",
        emissions.year: 1850
    }
}])
  1. API Filters

See all endpoints at http://localhost:4000/api-docs



Test urls:

Tech Stack

  • Node
  • Express
  • Cors
  • Morgan
  • Nodemon
  • Typescript
  • Mongoose
  • Multer
  • CSV-parser
  • Swagger-jsdoc
  • Swagger-ui-express

In case that docker get stuck on windows, run:

wsl -l -v
wsl --unregister docker-desktop

Other docker commands

# start/stop docker
docker-compose down
docker-compose up -d

# last running containers
docker ps

# show all containers
docker ps -a

# stop containers
docker stop $(docker ps -a -q)
docker stop e8

# remove containers
docker rm $(docker ps -aq)

# list images
docker images -a

# remove image(s)
docker rmi -f 23
docker rmi $(docker images -aq)

# volumes
docker volume ls
docker volume rm $(docker volume ls -q)