diff --git a/index.js b/index.js index a2fb6a964d92ff80a7faaf625c5ab02150cfb089..0816eecf56324db9987ac3a7beb157f1243583e4 100644 --- a/index.js +++ b/index.js @@ -4,27 +4,32 @@ var url = require('url'); var request = require('request'); // Use envars on Heroku -if ( process.env.NODE && ~process.env.NODE.indexOf("heroku") ) { +if ( process.env.WU_API_KEY ) { var config = require('./config-sample.json'); + + config.apikey = process.env.WU_API_KEY; } else { var config = require('./config.json'); } +if ( process.env.PORT ) { + config.port = process.env.PORT; +} + var format = ".json"; -var apikey = process.env.WU_ACCESS || config.apikey; var bodyParser = require('body-parser'); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); //use port is set in the environment variable, or 9001 if it isn’t set. -app.set( 'port', ( process.env.PORT || config.port ) ); +app.set( 'port', config.port ); //for testing that the app is running app.get('/', function(req, res){ var msg = 'Running!'; - if ( ! apikey.length ) { + if ( ! config.apikey.length ) { msg += ' Please set the API key.' } @@ -34,7 +39,7 @@ app.get('/', function(req, res){ //app.post is triggered when a POST request is sent to the URL ‘/post’ app.post('/post', function(req, res){ // Can't proceed without an API key - if ( ! apikey.length ) { + if ( ! config.apikey.length ) { return res.sendStatus( 500 ); } @@ -44,7 +49,7 @@ app.post('/post', function(req, res){ } var parsed_url = url.format({ - pathname: 'http://api.wunderground.com/api/' + apikey + '/conditions/q/' + req.body.text + format, + pathname: 'http://api.wunderground.com/api/' + config.apikey + '/conditions/q/' + req.body.text + format, }); console.log( req.body.text ); @@ -64,7 +69,7 @@ app.post('/post', function(req, res){ var stationID = conditionData.current_observation.station_id; parsed_url = url.format({ - pathname: 'http://api.wunderground.com/api/' + apikey + '/forecast/q/' + req.body.text + format, + pathname: 'http://api.wunderground.com/api/' + config.apikey + '/forecast/q/' + req.body.text + format, }); request( parsed_url, function( error, response, body ) {