[英]How do i use x-ray on a meteor app?
I'm trying to use x-ray on Meteor but so far with no luck. 我正在尝试在Meteor上使用X射线,但到目前为止还没有运气。
Here's the example I'm testing (it works fine on a basic node app) 这是我正在测试的示例(在基本节点应用上可以正常工作)
import Xray from 'x-ray';
var xray = new Xray();
xray('http://reddit.com/r/meteor/', '.title',
[{
title: '',
href: '@href'
}]
)
.write('./result.json');
I hope you figured it out since it's 5 months ago, I had my head around this question and figured out that way. 希望您早在5个月前就解决了这个问题,因此我想出了解决的办法。
Don't use the atmosphere package since it's not maintained anymore. 不要使用气压套件,因为它已不再维护。
$meteor npm install --save x-ray
( https://github.com/lapwinglabs/x-ray ) $meteor npm install --save x-ray
( https://github.com/lapwinglabs/x-ray )
Then just create a Meteor.method on the server side and call it on the client side. 然后只需在服务器端创建一个Meteor.method并在客户端调用它。
( https://docs.meteor.com/api/methods.html ) ( https://docs.meteor.com/api/methods.html )
// Server Side
import Xray from 'x-ray'
Meteor.methods({
scrap:function(){
var x = Xray();
console.log('Is scrapping');
x('http://google.com', 'title')(function(err, title) {
console.log(title) // Google
})
}
});
Then 然后
// Client Side
Meteor.apply('scrap', function(error, result) {
console.log(error, result);
console.log('Scrap is bussy');
})
Cheers 干杯
The code from the previous post indeed invokes the x-ray function on the server side but doesn't return the result to the client. 上一篇文章中的代码确实在服务器端调用了X射线功能,但没有将结果返回给客户端。
Using async/wait and promises (ES7) you can return the result from the server to the client: 使用异步/等待和承诺(ES7),您可以将结果从服务器返回到客户端:
method.js (server): method.js(服务器):
import { Meteor } from 'meteor/meteor';
import Xray from 'x-ray';
Meteor.methods({
async 'scrape.test'() {
let x = Xray(),
scraper;
function scrap() {
return new Promise((r, e) => {
x('http://google.com', 'title')(function(err, title) {
if (err) e(err);
if (title) r(title);
});
});
}
try {
return await scrap();
} catch (error) {
throw new Meteor.Error('500', error);
}
}
});
client.js: client.js:
Meteor.call('scrape.test', (error, result) => {
if (error) console.log('error', error);
console.log('result', result);
});
Cheers 干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.