0

My web api, server.js uses mongodb as its database and is dockerized in a docker container using docker-compose.

I am using chai/mocha to create test.js where test.js will be dockerized in a container and it will be used to test out my web api where my web api will run in localhost:8080 (not sure if that is the right way to do, I set server=http://localhost:8080). My test container will be exposed to port 80.

After running the mocha tests, I am being thrown an unhandled promise rejection, ECONNREFUSED exception where I am not able to connect to port 8080.

What am I doing wrong?

server.js:

const express = require("express");
const app = express();
const connectDb = require("./src/connection");
const Task = require("./src/Task.model");

const PORT = 8080;

app.use(express.urlencoded({ extended: false }));

app.get("/tasks", async (req, res) => {
  const tasks = await Task.find();
  res.json(tasks);
});

app.post("/task-create", async (req, res) => {

  const task = new Task(
    {
      title: req.query.title,
      description: req.query.description,
      start: req.query.start,
      end: req.query.end,
      priority: req.query.priority,
      category: req.query.category,
      status: req.query.status
    });

  await task.save().then(() => console.log("Task created"));

  res.send(task);
});

app.listen(PORT, function() {
  console.log(`Listening on ${PORT}`);

  connectDb().then(() => {
    console.log("MongoDb connected");
  });
});

test.js

let chai = require("chai");
let chaiHttp = require("chai-http");
let server="http://localhost:8080"
chai.use(chaiHttp);

describe ("CRUD OPERATIONS", () => {

    it ("Should add task in DB", (done) => {
        chai.request(server)
            .post("/task-create")
            .send({
                title: "Read a book",
                description: "Quiet activity",
                start: "2020/03/02",
                end: "2020/03/06",
                priority: "high",
                category: "education",
                status: false
            })
            .end((err, result) => {
                result.should.have.status(200);
                console.log("result is: " + result.body)

                done()
            })
    })

    it ("Should Fetch all the tasks", (done) => {
        chai.request(server)
            .get("/tasks")
            .end((err, result) => {
                if ("error is: " + err) {
                    console.log(err)
                }
                result.should.have.status(200);
                console.log("result is: " + result.body)

                done()
            })
    })
})

ECONNREFUSED

DanCode
  • 525
  • 3
  • 7
  • 25
  • You are running your api and tests from a different containers ? – theBittor Apr 01 '20 at 01:16
  • Yes, the api and tests are from different containers. – DanCode Apr 01 '20 at 01:20
  • Ok. From your test container `localhost:8080` will not resolve to your api container. Take a look at this [answer](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – theBittor Apr 01 '20 at 01:23
  • @Victor, if I specify the --network=host option, the same thing happened. The command I entered is docker run -it --network=host client-image – DanCode Apr 01 '20 at 01:27
  • which is your version of docker and on what platform ? I test this with `docker 19.03.5` on linux and it works. – theBittor Apr 01 '20 at 01:48
  • Hi victor, it worked. I think I misspelled a command. – DanCode Apr 01 '20 at 01:49
  • I got another problem, that is, the mongodb is not capturing the parameters sent from the test. mongodb is able to capture the object though. – DanCode Apr 01 '20 at 01:56

0 Answers0