Skip to content

Instantly share code, notes, and snippets.

@Adurtxi
Last active August 9, 2021 10:14
Show Gist options
  • Select an option

  • Save Adurtxi/863ad3db34f712b3e65a8d1a64a6cbfb to your computer and use it in GitHub Desktop.

Select an option

Save Adurtxi/863ad3db34f712b3e65a8d1a64a6cbfb to your computer and use it in GitHub Desktop.
Database Config of NodeJS Backend wih Express and MySQL using DotEnv
'use strict';
const mysql = require('mysql');
//- NEW
require('dotenv').config();
const { DB_HOST, DB_USER, DB_PASS, DB_DATABASE } = process.env;
//-
//- UPDATED
const dbConnection = mysql.createConnection({
host : DB_HOST,
user : DB_USER,
password : DB_PASSWORD,
database : DB_DATABASE
});
//-
dbConnection.connect((err) => {
if (err) throw err;
console.log('Database Connected!');
});
module.exports = dbConnection;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment