Interacting with External Sources

A common use of the HTTP services in Node.js is to access external systems to get data to fulfill client requests. A variety of external systems provide data that can be used in various ways. In this example, the code connects to the openweathermap.org API to retrieve weather information about a city. To keep the example simple, the output from openweathermap.org is pushed to the browser in a raw format. In reality, you would likely massage the pieces of data needed into your own pages, widgets, or data responses.

Listing 7.7 shows the implementation of a web service that accepts both GET and POST requests. For GET requests, a simple webpage with a form is returned that allows the user to post a city name. Then in the POST request, the city name is accessed, and the Node.js web client starts up and connects remotely to openweathermap.org to retrieve weather info for that city. Then that info is returned back to the server, along with the original web form.

The big difference between this example and the previous examples is that the webserver also implements a local web client to connect to the external service and get data to formulate the response. The webserver is implemented in lines 35–49. Notice that if the method is POST, we read the form data from the request stream and use querystring.parse() to get the city name and call into the getWeather() function.

The getWeather() function in lines 26–34 implements the client request to openweathermap.org. Then the parseWeather() request handler in lines 17–25 reads the response from openweathermap.org and passes that data to the sendResponse() function defined in lines 4–16, which formulates the response and sends it back to the client. Figure 7.5 shows the implementation of the external service in a web browser.

Listing 7.7 http_server_external.js: Implementing an HTTP web service that connects remotely to a an external source for weather data


01 var http = require('http'),
02 var url = require('url'),
03 var qstring = require('querystring'),
04 function sendResponse(weatherData, res){
05   var page = '<html><head><title>External Example</title></head>' +
06     '<body>' +
07     '<form method="post">' +
08     'City: <input name="city"><br>' +
09     '<input type="submit" value="Get Weather">' +
10     '</form>';
11   if(weatherData){
12     page += '<h1>Weather Info</h1><p>' + weatherData +'</p>';
13   }
14   page += '</body></html>';
15   res.end(page);
16 }
17 function parseWeather(weatherResponse, res) {
18   var weatherData = '';
19   weatherResponse.on('data', function (chunk) {
20     weatherData += chunk;
21   });
22   weatherResponse.on('end', function () {
23     sendResponse(weatherData, res);
24   });
25 }
26 function getWeather(city, res){
27   var options = {
28     host: 'api.openweathermap.org',
29     path: '/data/2.5/weather?q=' + city
30   };
31   http.request(options, function(weatherResponse){
32     parseWeather(weatherResponse, res);
33   }).end();
34 }
35 http.createServer(function (req, res) {
36   console.log(req.method);
37   if (req.method == "POST"){
38     var reqData = '';
39     req.on('data', function (chunk) {
40       reqData += chunk;
41     });
42     req.on('end', function() {
43       var postParams = qstring.parse(reqData);
44       getWeather(postParams.city, res);
45     });
46   } else{
47     sendResponse(null, res);
48   }
49 }).listen(8080);


Image

Figure 7.5 Implementing an external web service that connects to a remote source for weather data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset