Skip to content

Instantly share code, notes, and snippets.

@chewtoys
Created October 2, 2019 01:27
Show Gist options
  • Save chewtoys/3df02a3aeaf8a9b04373ae9b52086e6b to your computer and use it in GitHub Desktop.
Save chewtoys/3df02a3aeaf8a9b04373ae9b52086e6b to your computer and use it in GitHub Desktop.

npm install dotenv

/.env

NODE_ENV=development
PORT=4000
MONGO_URI=mongodb://localhost:27017/dev

/config/vars.js

const path = require('path')

// import .env variables
require('dotenv-safe').load({
  path: path.join(__dirname, '../../.env'),
})

module.exports = {
  env: process.env.NODE_ENV,
  port: process.env.PORT,
  mongo: {
    uri: process.env.MONGO_URI,
  },
}

/index.js

const {
  port,
  env,
} = require('./config/vars')

// listen to requests
app.listen(port, () => console.log(`Server connection success: ${port} ( ${env} )`))

/**
 * Exports express
 * @public
 */
module.exports = app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment