Skip to content

Instantly share code, notes, and snippets.

@jacksonfdam
Last active February 20, 2017 22:33
Show Gist options
  • Save jacksonfdam/f37df719c2d41f138a8c66e7fbfd28e4 to your computer and use it in GitHub Desktop.
Save jacksonfdam/f37df719c2d41f138a8c66e7fbfd28e4 to your computer and use it in GitHub Desktop.
Exem
npm install --save mongodb request request-promise
var request = require('request');
var rp = require('request-promise');
var db = require('mongodb').MongoClient;
let key = '246bf886104d519a1d2bf62aef1054ff';
router.get('/update-em-cartaz', function(req, res, next) {
rp('http://api.themoviedb.org/3/movie/now_playing?page=1&api_key='+key)
.then(function (parsedBody) {
var jsonData = JSON.parse(parsedBody);
db.connect('mongodb://localhost:27017/moviefun', function(err, db) {
if (err) {
throw err;
}
db.collection('filmes').insert(jsonData.results);
});
res.json({
title:'filmes',
movies:jsonData.results
});
})
.catch(function (err) {
// Crawling failed...
});
});
router.get('/emcartaz', function(req, res, next) {
db.connect('mongodb://localhost:27017/moviefun', function(err, db) {
if (err) {
console.error(err);
}
db.collection('filmes').find().limit(100).toArray(function(err, result) {
if (err) {
console.error(err);
}
res.render('filmes',{
title:'Em Cartaz',
filmes:result
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment