简体   繁体   中英

Need help making 2 API calls within same Node.JS/Express route

I'm just practicing with my first API call in Node.JS. Using YahooWeather's API to create a customized weather page. Weather works, but I also want to get current local time of target location, which it does not provide. It does, however, provide the zone name (eg America/New York), which I'd like to plug into a 2nd API called Timezone.db that "can" give local time based on a given zone name. I'm just not sure how to feed info from the YahooWeather API into the Timezone.db API, so I can then send both data variables to my home template page to process for displaying. Planning to use the local time to dynamically switch to a night image page background if it's after sunset time at target location.

I have the YahooWeather API working so far, and here's the code for that just not sure where to fit in the 2nd API call to timezone, using the timezone extracted from Yahoo Weather API. An example request to the timezone db API would look like this:

http://api.timezonedb.com/v2.1/get-time-zone?key=#########&format=json&by=zone&zone=America/New

var express = require("express"); 
var app = express();
var bodyParser = require("body-parser");
var location = "";

app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");


app.get("/", function(req, res){  // landing page route
    location = req.query.search;

    if (location === undefined){
        location = "paso robles, ca"; `// default location if left undefined`
        //console.log("location = ", location);
    }

var OAuth = require('oauth');
var header = {
    "X-Yahoo-App-Id": "########"
};
var request = new OAuth.OAuth(
    null,
    null,
    '###########################################################################################',
    '#######################################',
    '1.0',
    null,
    'HMAC-SHA1',
    null,
    header
);
request.get(
    'https://weather-ydn-yql.media.yahoo.com/forecastrss?location=' + location + '&format=json',
    null,
    null,
    function (err, data, result) {
        if (err) {
            console.log(err);
            return;
        } else {
            parsedData = JSON.parse(data); 
            console.log("Parsed Data = ", parsedData);
            res.render("home", {parsedData: parsedData});
        }
    }
);
});

Any help you guys can give this noob would be greatly appreciated. -Thanks!

You can always request to timezone inside your weather request callback

request.get(
    'https://weather-ydn-yql.media.yahoo.com/forecastrss?location=' + location + '&format=json',
    null,
    null,
    function (err, data, result) {
        if (err) {
            console.log(err);
            return;
        } else {
            parsedData = JSON.parse(data); 
            console.log("Parsed Data = ", parsedData);

            request.get(`timezone url`,function (err, data, result) {
                let timezone = data
                res.render("home", {parsedData: parsedData});
            });   
        }
    }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM