Помогите мне, пожалуйста, я чувствую, что это что-то с res.json ().Это работает, если я позвоню после первого запроса, но не второго.Реальное приложение сейчас довольно простое: оно просто удаляет некоторые пользовательские данные из социальных сетей, таких как Twitter или Instagram, а затем возвращает объект json.Спасибо!
app.get("/", function(req, res) {
let twitterHandle = req.query.twitter;
let instagramHandle = req.query.instagram;
let twitterURL = "https://twitter.com/" + twitterHandle + "?lang=en";
let instagramURL = "https://instagram.com/" + instagramHandle;
var json = {};
console.log(twitterHandle);
console.log(instagramHandle);
// The structure of our request call
// The first parameter is our URL
// The callback function takes 3 parameters, an error, response status code and the html
if (twitterHandle != "") {
request(twitterURL, function(error, response, html) {
// First we'll check to make sure no errors occurred when making the request
if (!error) {
// Next, we'll utilize the cheerio library on the returned html which will essentially give us jQuery functionality
var $ = cheerio.load(html);
// Finally, we'll define the variable we're going to capture
// We'll be using Cheerio's function to single out the necessary information
// using DOM selectors which are normally found in CSS.
var twitterFollowers = $(
"#page-container > div.ProfileCanopy.ProfileCanopy--withNav.ProfileCanopy--large.js-variableHeightTopBar > div > div.ProfileCanopy-navBar.u-boxShadow > div.AppContainer > div > div.Grid-cell.u-size2of3.u-lg-size3of4 > div > div > ul > li.ProfileNav-item.ProfileNav-item--followers > a"
)
.text()
.replace(/\D/g, "");
var twitterFollowing = $(
"#page-container > div.ProfileCanopy.ProfileCanopy--withNav.ProfileCanopy--large.js-variableHeightTopBar > div > div.ProfileCanopy-navBar.u-boxShadow > div.AppContainer > div > div.Grid-cell.u-size2of3.u-lg-size3of4 > div > div > ul > li.ProfileNav-item.ProfileNav-item--following > a"
)
.text()
.replace(/\D/g, "");
// And now, the JSON format we are going to expose
json[twitterFollowers] = twitterFollowers;
json[twitterFollowing] = twitterFollowing;
// Send the JSON as a response to the client
}
});
}
if (instagramHandle != "") {
request(instagramURL, function(error, response, html) {
// First we'll check to make sure no errors occurred when making the request
if (!error) {
// Next, we'll utilize the cheerio library on the returned html which will essentially give us jQuery functionality
var $ = cheerio.load(html);
// Finally, we'll define the variable we're going to capture
// We'll be using Cheerio's function to single out the necessary information
// using DOM selectors which are normally found in CSS.
var instagramFollowers = "chicken";
var instagramFollowing = "chicken";
// And now, the JSON format we are going to expose
json.instagramFollowers = instagramFollowers;
json.instagramFollowing = instagramFollowing;
// Send the JSON as a response to the client
}
});
}
res.json(json);
});
app.listen(process.env.PORT || 3000);
module.exports = app;