Fetching weather data with Node.js and storing it to MongoDB

My initial idea was to take first steps with Node.js and implement a simple app fetching weather related data and store it to a MongoDB based database. As prerequisites Node.js and MongoDB were already installed and running on the server.

I had few objectives in my mind:

  1. Implement a simple console app with Node.js.
  2. Learn the basics of using MongoDB: create a database with a schema and store simple data to it.

To keep things simple at this stage I registered an account in World Weather Online (http://free.worldweatheronline.com/) and obtained a free API key for testing. The data provided is available as JSON which simplified things greatly. I studied the API and decided to use a hardcoded URL at this stage.

// The API Key in the following is obfuscated. 
// In case you want to use the API, register an own account.
var location = 'Bremen,Germany';
var reqUrl = 'http://free.worldweatheronline.com/feed/weather.ashx?q='
+location +'&format=json&num_of_days=2&key=xxxxxxxxxxxxxxxxxxxxxx';

Again, to simplify things, I decided to install the “request” library to Node.js. It makes the life easier when one wants to make HTTP requests from Node.js.

// Send a request to the uri, then parse JSON and store the data 
// (location, temperature and the time of observation 
var request = require("request");
request({
   uri: reqUrl,
   method: "GET",
   timeout: 10000,
   followRedirect: true,
   maxRedirects: 10
}, function(error, response, body) {
   var parsedJSON = JSON.parse(body);
   var currTemp = parsedJSON.data.current_condition[0].temp_C;
   var obsTime = parsedJSON.data.current_condition[0].observation_time;
   storeTemperature(location, currTemp, obsTime);
});

// Store location, temperature and time of the observation
function storeTemperature(loc, temp, obstime) {
   var mongoose = require('mongoose');
   // connect to a db at localhost
   mongoose.connect('localhost', 'temperatures');
   // define the db schema
   var schema = mongoose.Schema({ 
      location: String, 
      temperature: String, 
      observationTime: String 
   });

   var Temperature = mongoose.model('Temperature', schema);
   // Create a new object with the fields initialized by the read data
   var t = new Temperature({
      location: loc, 
      temperature: temp, 
      observationTime: obstime
   })
   // attempt to save the data
   t.save(function(err) {
     if (err) {
        console.log("error saving"); 
     } else {
        console.log("saved");
        throw '';
     }
 });
}

After the implementation I executed the program a few times to ensure that it worked:

node ./store_temperature.js

Moreover, I wrote a script calling the Node.js app and configured the crontab to run the script every hour. So now I have an app gathering temperature stats and those could possibly be visualized at some point. But now it’s time to learn more MongoDB and Node.js and return later with a new post.

Leave a Reply

Your email address will not be published. Required fields are marked *