'Mongoose is unable to connect with DB running on Kubernetes: ECONNREFUSED 127.0.0.1:27017

I have a MongoDB running in a Kubernetes Pod. The Pod is forwarded to my 27017 Port on localhost. Opening localhost:21017 returns It looks like you are trying to access MongoDB over HTTP on the native driver port.. Additionaly I can connect to my DB via DataGrip.

However, when I try to create a connection with node.js it fails with the said error: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017

Checking my running services under Task-Manager -> Services displays no MongoDB. However I assume that this is right since my DB is running under Kubernetes on a Remote Server. Right?

Can someone tell me what I'm doing wrong?

Here's my code:

const mongoose = require('mongoose')

mongoose.connect('mongodb://127.0.0.1:27017/testing')
    .catch(error => {
        console.log("Error with message: " + error.message)
    })

Update:

The above code works, if it's being executed in VSCode. When I try to run it in WebStorm it somehow doesn't. If anyone has made the same experience and knows the cause for this I'd love to know.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source