skip to Main Content

Sorry in advanced If this question did not include the proper tags.

I’m trying to build out a CRUD API using MySQL in NodeJS. I want to pull my DB credentials from the enviromental variables. Right Now I’m trying to get this working in my local environment. But eventually I will be deploying this to a production environment hosted on A2Hosting

The following is my workflow to connect to the database.

First everything works fine if I hardcode my credentials in my db.config.js file as shown below (note: I redacted the user, password, and db values)

module.exports = {
    HOST: "localhost",
    USER: "{username}",
    PASSWORD: "{password}",
    DB: "{database}"
  };

and my Database connection is as follows

const mysql = require("mysql");
const dbConfig = require("../config/db.config.js");

require('dotenv').config()

var connection = mysql.createPool({
  host: dbConfig.HOST,
  user: dbConfig.USER,
  password: dbConfig.PASSWORD,
  database: dbConfig.DB
});

module.exports = connection;

Everything works fine as I can access the routes to do my crud example the below is the get route to show the books

Browser view with all database rows

I Than went ahead and created a .env file in the root of the app. where I store my credentials.

.env file (again actual values are redacted)

  HOST='localhost',
  USER='{username}',
  PASSWORD='{password}',
  DB='{database}'

When I try to replace my db.config.js values with the environment values, I get a ENOTFOUND message when I try to navigate to the api/tutorials get route

db.config.js (modified to use the env variables)

import('dotenv').config()

module.exports = {
    HOST: process.env.HOST,
    USER: process.env.USER,
    PASSWORD: process.env.PASSWORD,
    DB: process.env.DB
  };

and here is the browser message that I get the after trying to use env variables.

Browser view with error response message

Thanks in advanced for any help in this.

–Dean

2

Answers


  1. Chosen as BEST ANSWER

    As it turns out, the tutorial that I have been following to build out this API is out of date. and I started to look towards another tutorial.

    It turns out that the original tutorial was using the mysql npm package and not the newer updated mysql2 npm package. I'm not saying that's the reason why the environment variables are not working, but it seems that the code from the newer tutorial that I'm following has pretty much the same method for connectiong to MySQL with using mysql2 as the only exception.


  2. Whenever I use dotenv in my apps it is given a filename. Like this:

    require('dotenv').config({ path: '.env.dev' });
    

    seems logical that not giving the filename results in the error you get.

    Kinda wondering why you choose to use db.config.js? Why not read the values directly when you create your connection:

    var connection = mysql.createPool({
      host: process.env.HOST,
      user: process.env.USER,
      password: process.env.PASSWORD,
      database: process.env.DB
    });
    

    Seems to me a bit redundant to have an extra module for that.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search