簡體   English   中英

如何在流星應用程序上使用X射線?

[英]How do i use x-ray on a meteor app?

我正在嘗試在Meteor上使用X射線,但到目前為止還沒有運氣。

這是我正在測試的示例(在基本節點應用上可以正常工作)

import Xray from 'x-ray';

var xray = new Xray();

xray('http://reddit.com/r/meteor/', '.title',
[{
  title: '',
  href: '@href'
}]
)
  .write('./result.json');

希望您早在5個月前就解決了這個問題,因此我想出了解決的辦法。

不要使用氣壓套件,因為它已不再維護。

$meteor npm install --save x-rayhttps://github.com/lapwinglabs/x-ray

然后只需在服務器端創建一個Meteor.method並在客戶端調用它。

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
    })
   }
});

然后

// Client Side

Meteor.apply('scrap', function(error, result) {
  console.log(error, result);
  console.log('Scrap is bussy');
})

干杯

上一篇文章中的代碼確實在服務器端調用了X射線功能,但沒有將結果返回給客戶端。

使用異步/等待和承諾(ES7),您可以將結果從服務器返回到客戶端:

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:

Meteor.call('scrape.test', (error, result) => {
  if (error) console.log('error', error);
  console.log('result', result);
});

干杯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM