diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..ef9fd1d --- /dev/null +++ b/.env.example @@ -0,0 +1,9 @@ +# .env.example +NODE_ENV=development +PORT=8000 + +# Set your database connection information here +DATABASE_HOST=192.168.56.111 +DATABASE_NAME=app1 +DATABASE_USER=homestead +DATABASE_PASSWORD=secret \ No newline at end of file diff --git a/.gitignore b/.gitignore index ccb2c80..9a30b15 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ -package-lock.json \ No newline at end of file +package-lock.json +.env \ No newline at end of file diff --git a/app.js b/app.js index 49349df..3da43c6 100755 --- a/app.js +++ b/app.js @@ -2,16 +2,21 @@ const app = require('express')(); const mysql = require('mysql'); const bodyParser = require('body-parser'); + +//Use dotenv to read .env vars into Node +require('dotenv').config(); + const routes = require('./routes'); // the mysql.createConnection function takes in a configuration object which contains host, user, password and the database name. const db = mysql.createConnection ({ - host: 'localhost', - user: 'root', - password: 'root', - database: 'app1' + host: process.env.DATABASE_HOST, + user: process.env.DATABASE_USER, + password: process.env.DATABASE_PASSWORD, + database: process.env.DATABASE_NAME }); + // connect to database db.connect((err) => { if (err) { @@ -22,7 +27,7 @@ db.connect((err) => { global.db = db; -const PORT = process.env.PORT || 3000; +const PORT = process.env.PORT || 8000; app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); // parse form data client diff --git a/package.json b/package.json index 6f05bb2..84c8055 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "license": "ISC", "dependencies": { "body-parser": "^1.18.3", + "dotenv": "^6.2.0", "ejs": "^2.6.1", "express": "^4.16.4", "mysql": "^2.16.0",