Skip to content

Commit 19280dc

Browse files
authored
Merge pull request #2 from thebhandariprakash/B-2
.env file added.
2 parents bf93679 + 4c338bd commit 19280dc

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

.env.example

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# .env.example
2+
NODE_ENV=development
3+
PORT=8000
4+
5+
# Set your database connection information here
6+
DATABASE_HOST=192.168.56.111
7+
DATABASE_NAME=app1
8+
DATABASE_USER=homestead
9+
DATABASE_PASSWORD=secret

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
2-
package-lock.json
2+
package-lock.json
3+
.env

app.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22
const app = require('express')();
33
const mysql = require('mysql');
44
const bodyParser = require('body-parser');
5+
6+
//Use dotenv to read .env vars into Node
7+
require('dotenv').config();
8+
59
const routes = require('./routes');
610

711
// the mysql.createConnection function takes in a configuration object which contains host, user, password and the database name.
812
const db = mysql.createConnection ({
9-
host: 'localhost',
10-
user: 'root',
11-
password: 'root',
12-
database: 'app1'
13+
host: process.env.DATABASE_HOST,
14+
user: process.env.DATABASE_USER,
15+
password: process.env.DATABASE_PASSWORD,
16+
database: process.env.DATABASE_NAME
1317
});
1418

19+
1520
// connect to database
1621
db.connect((err) => {
1722
if (err) {
@@ -22,7 +27,7 @@ db.connect((err) => {
2227

2328
global.db = db;
2429

25-
const PORT = process.env.PORT || 3000;
30+
const PORT = process.env.PORT || 8000;
2631

2732
app.use(bodyParser.urlencoded({ extended: false }));
2833
app.use(bodyParser.json()); // parse form data client

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"license": "ISC",
1414
"dependencies": {
1515
"body-parser": "^1.18.3",
16+
"dotenv": "^6.2.0",
1617
"ejs": "^2.6.1",
1718
"express": "^4.16.4",
1819
"mysql": "^2.16.0",

0 commit comments

Comments
 (0)