私はnodejsとexpressjsを教えようとしていますが、javaやC++から来ていますが、これは慣れるのが難しいことが分かります。nodejsに依存する機能を管理する方法
私は、特定の郵便番号の天気予報を返すことになっている単純で面倒なモジュールを作った。
これは、ユーザーの郵便番号を取得し、その郵便番号の地理座標を生成するためにGoogle APIを使用することです。私はJASONファイルから座標を取得し、次のAPI呼び出しに提供します。この呼び出しはforecast.io APIに行われ、今回はその場所の天気データもJASONファイルから取得されます。
JavaScriptにはあまり書かれていませんが、私はこれらの2つの機能をお互いに待たせるのに苦労しています。この場合、私は最初に終了するためにgoogle api呼び出しが必要です2番目のapi呼び出しに必要です。
誰かがこのコードを見て、私が使用した戦略が正しいかどうか教えてください。このような状況でjavascriptで何が行われているかを知るための提案を提供してください。 ここにコード:
// The required modules.
var http = require("http");
var https = require("https");
//result object
var resultSet = {
latitude :"",
longitude:"",
localInfo:"",
weather:"",
humidity:"",
pressure:"",
time:""
};
//print out error messages
function printError(error){
console.error(error.message);
}
//Forecast API required information:
//key for the forecast IO app
var forecast_IO_Key = "this is my key, not publishing for security reasons";
var forecast_IO_Web_Adress = "https://api.forecast.io/forecast/";
//Create Forecast request string function
function createForecastRequest(latitude, longitude){
var request = forecast_IO_Web_Adress + forecast_IO_Key + "/"
+ latitude +"," + longitude;
return request;
}
//Google GEO API required information:
//Create Google Geo Request
var google_GEO_Web_Adress = "https://maps.googleapis.com/maps/api/geocode/json?address=";
function createGoogleGeoMapRequest(zipCode){
var request = google_GEO_Web_Adress+zipCode + "&sensor=false";
return request;
}
function get(zipCode){
// 1- Need to request google for geo locations using a given zip
var googleRequest = https.get(createGoogleGeoMapRequest(zipCode), function(response){
//console.log(createGoogleGeoMapRequest(zipCode));
var body = "";
var status = response.statusCode;
//a- Read the data.
response.on("data", function(chunk){
body+=chunk;
});
//b- Parse the data.
response.on("end", function(){
if(status === 200){
try{
var coordinates = JSON.parse(body);
resultSet.latitude = coordinates.results[0].geometry.location.lat;
resultSet.longitude = coordinates.results[0].geometry.location.lng;
resultSet.localInfo = coordinates.results[0].address_components[0].long_name + ", " +
coordinates.results[0].address_components[1].long_name + ", " +
coordinates.results[0].address_components[2].long_name + ", " +
coordinates.results[0].address_components[3].long_name + ". ";
}catch(error){
printError(error.message);
}finally{
connectToForecastIO(resultSet.latitude,resultSet.longitude);
}
}else{
printError({message: "Error with GEO API"+http.STATUS_CODES[response.statusCode]})
}
});
});
function connectToForecastIO(latitude,longitude){
var forecastRequest = https.get(createForecastRequest(latitude,longitude),function(response){
// console.log(createForecastRequest(latitude,longitude));
var body = "";
var status = response.statusCode;
//read the data
response.on("data", function(chunk){
body+=chunk;
});
//parse the data
response.on("end", function(){
try{
var weatherReport = JSON.parse(body);
resultSet.weather = weatherReport.currently.summary;
resultSet.humidity = weatherReport.currently.humidity;
resultSet.temperature = weatherReport.currently.temperature;
resultSet.pressure = weatherReport.currently.pressure;
resultSet.time = weatherReport.currently.time;
}catch(error){
printError(error.message);
}finally{
return resultSet;
}
});
});
}
}
//define the name of the outer module.
module.exports.get = get;
は正しく返品されていますか?ここで私の使用は最終的に適切ですか?私はJavaのバックグラウンドから来て、Javaではクロージャーコードを実行するためにtry {} catch(){}とfinally {}ブロックを使用するのが問題ないことに注意してください。しかし、私はいくつかのExpressを組み込み、別のモジュールからこのモジュールのメソッドを実行しようとしましたが、私が得ているのは未定義の戻り値です。