dotenv results in an ERR_CONNECTION_REFUSED

Viewed 27

I want to access my database and for that I need a password, etc. I use dotenv to achieve this.

const dotenv = require('dotenv').config({ path: 'config/.env' });

When i start the app with nodemon, the following Output appears:

enter image description here

Everything is fine. The 'testdb' is a value from the .env-File. Now when i go on my localhost:3000 the following Error appears:

enter image description here

If I comment out dotenv the error is gone.

enter image description here

In Visual Studio it works in Debug and Releases mode.

Here are the important files:

enter image description here

app.js

const express = require('express');
const path = require('path');

const DB = require('./Services/DB');

const PORT = 3000;
const app = express();
const router = express.Router();

router.get('/', function (req, res) {
    res.sendFile(path.join(__dirname + '/HTML_Files/index.html'));
});

app.use('/', router);
app.listen(process.env.port || PORT);

console.log(`Running at Port ${PORT}`);

Svc_BASE.js

const Sequelize = require("sequelize");
const dotenv = require('dotenv').config({ path: 'config/.env' });

const Models = require('../Models/init-models');

class Svc_BASE {
    constructor() {
        console.log(process.env.DATABASE);
    }
}
module.exports = Svc_BASE;

Svc_UA_LOBSTER

const Svc_BASE = require('./Svc_BASE.js');

class Svc_UA_LOBSTER extends Svc_BASE {
    constructor() {
        super();
    }
}
module.exports = Svc_UA_LOBSTER;

DB.js

const Svc_UA_LOBSTER = require('./Svc_UA_LOBSTER');
const SvcUA_LOBSTER = new Svc_UA_LOBSTER();
exports.SvcUA_LOBSTER = SvcUA_LOBSTER;

index.html

<html>
<head>
    <title>Express HTML</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js">        </script>
    <link rel="stylesheet"     href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js">    </script>
</head>
<body>
    <div style="margin:100px;">

        <nav class="navbar navbar-inverse navbar-static-top">
        <div class="container">
            <a class="navbar-brand" href="/">Express HTML</a>
            <ul class="nav navbar-nav">
                <li class="active">
                    <a href="/">Home</a>
                </li>
                <li>
                    <a href="/about">About</a>
                </li>
                <li>
                    <a href="/sitemap">Sitemap</a>
                </li>
            </ul>
        </div>
        </nav>
        <div class="jumbotron" style="padding:40px;">
            <h1>Hello, world!</h1>
            <p>This is a simple hero unit, a simple jumbotron-style component for calling extra attention to featured content or information.</p>
            <p><a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a></p>
        </div>
    </div>
</body>
</html>

I don't know why this happen. Can anyone help me?

Many thanks in advance.

0 Answers
Related