node.js - NodeJS: Use environment variables in front end scripts -
i'm writing socket.io app, , i'm getting annoyed @ having chance line in frontend scripts.js file every time push heroku, from
var socket = io.connect('http://localhost');
to
var socket = io.connect('http://ersatz-elephant-1337.herokuapp.com/');
i know can use process.env.var
access variables on backend, i'm wondering if there's way programmatically determine use in frontend scripts? i'd rather leave scripts static aside this, maybe that's not possible?
edit: i'm not looking solution on backend... i'm looking way /public/scripts.js
file can connect right thing. guess maybe way specifying special in server.js
frontend script file, rather serving statically, if so, that's instruction i'm looking for, not how access env vars on server. sorry confusion!
it's possible. either use foreman or use configuration script one:
var url = require('url'); var config = {}; var dburl; if (typeof(process.env.database_url) !== 'undefined') { dburl = url.parse(process.env.database_url); } else { dburl = url.parse('tcp://postgres:postgres@127.0.0.1:5432/db'); } config.dialect = 'postgres'; config.protocol = dburl.protocol.substr(0, dburl.protocol.length - 1); // remove trailing ':' config.username = dburl.auth.split(':')[0]; config.password = dburl.auth.split(':')[1]; config.host = dburl.hostname; config.port = dburl.port; config.database = dburl.path.substring(1); console.log('using database ' + config.database); module.exports = config;
Comments
Post a Comment