I want to set an express server that returns the results of the queries of an array of items. I have read in this question that I can use Promise.each
for this...
What I intend my code to do is:
- Web Scrape a page with a list of movies and return the titles as an array.
- Use the title of the movies to do a request of an API for each
- Return a response to the client with the results of the API response to every movie
Here's my code:
var express = require('express');
var app = express();
var Xray = require('x-ray');
var x = Xray();
app.get('/', function (req,res){
var username = 'medicengonzo';
var pages = 3;
x('https://letterboxd.com/'+username+'/watchlist/', 'li.poster-container', [{
movie: 'img@alt'
}])(function (err,results){
console.log('Entered x-ray callback fn');
results.forEach(function(result, index){
console.log('Iteration');
console.log(results.length);
request('http://netflixroulette.net/api/api.php?title='+encodeURIComponent(result.movie),
function(err, response, body){
movies = [];
console.log(count);
obj = JSON.parse(body);
if(obj.errorcode != 404){
movies.push('Movie found: '+obj.show_title, 'ID: '+obj.show_id);
}
else{
movies.push('No movie found');
}
res.send(movies);
}
);
});
})
.paginate('.paginate-current+li a@href')
.limit(pages);
});
app.listen(3000, function(){
console.log('Listening on port 3000!');
});