javascript - read .env file and convert all lines into an object -


i'm trying read .env file foreman using read env variables might want have when starting node server.

i'm using:

var path = require("path"),     _ = require("underscore"),     fs = require("fs"),     variables = fs.readfilesync(path.resolve(__dirname, ".env"), "utf8"); 

at point have string containing this:

node_env=development port=8080 

i convert string object can read this:

{     node_env: "development",     port: 8080 } 

i'm not sure how it. thinking of regex have no clue how read line line. or how type of variable ? can detect if it's string or number ( thinking see if there numbers means it's number ? ) ?

string.replace primary means simple parsing:

var env = {} variables.replace(/(\w+)=(.+)/g, function($0, $1, $2) { env[$1] = $2 }) 

to convert numeric values 8080 numbers,

variables.replace(/(\w+)=((\d+)|.+)/g, function($0, $1, $2, $3) {     env[$1] = $3 ? number($3) : $2; }); 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -