express-myvariables

1.0.4 • Public • Published

express-myvariables

Connect/Express middleware that provides convenient access to environment variables.

Usage

Configuration is straightforward and like other middleware.

// app.js
...
var expressMyVariables = require('express-myvariables');

...
  
app.use(expressMyVariables());
...

express-myvariables extends the request object with an object envVars. This way req.envVars.get('VARIABLE_NAME'); can be called anywhere in routers during request/response life cycle:

// myroute.js
var express = require('express');
var router = express.Router();

// GET event
router.get('/', function(req, res, next) {
  res.json({'Greeting': 'Hello World.', 'SUPER_SECRET_KEY': req.envVars.get("SUPER_SECRET_KEY")});
});

module.exports = router;

It will read from one of three files in '/env' (depending on NODE_ENV set)

production.env
development.env
testing.env

And return the value found there

{'Greeting': 'Hello World.', 'SUPER_SECRET_KEY': 'A98J39FJWEUHFOAW8HWEOI8F'}

express-myvariables has one optional parameter that can be provided inside a hash {'writeFiles': false}. The default is true. When true, the .gitignore file will be appended with 'env/' and three files will created in a directory ./env: production.env, testing.env and development.env ** be careful ** if you set writeFiles to false and attempt to access a file that doesn't exist, this will result in an app-crashing error.

// app.js
...
var expressMyVariables = require('express-myvariables');

...
  
app.use(expressMyVariables({'writeFiles': false}));
...

Package Sidebar

Install

npm i express-myvariables

Weekly Downloads

1

Version

1.0.4

License

MIT

Last publish

Collaborators

  • aykutgedik