2014-12-07 44 views
1

我想讓我的角/節點應用程序呈現動態打開圖元內容。Facebook OG元與角和節點

我一直在試着按照這個教程http://www.codewarmer.com/posts/1394433236-configuring-angularjs-nodejs-for-search-bots#!我有一些問題,幻影與節點工作

,我的問題似乎是與此類似Error message when using PhantomJS, breaks at random intervals

,除了我的錯誤不會在隨機時間間隔發生,它發生的時間。

編輯:這裏是我的代碼

在我server.js我需要的模塊,我創建基於上述嘖嘖叫PhantomHandler.js,它被稱爲像這樣。

var crawler = require('./modules/PhantomHandler'); 

這是PhantomHandler.js樣子:

var phantom = require('phantom'); 
var models = require('../models'); 

mongoose = require('mongoose'); 
Snapshot = models.Snapshot; 

url = require('url'); 
var baseUrl = 'my url'; 

function crawlSite(idx, arr, page, callback) { 
    crawlUrl(arr[idx], page, function(data) { 
     data.links.forEach(function(link) { 
      if (arr.indexOf(link) < 0) 
       arr.push(link); 
     }); 
     Snapshot.upsert(data); 

     if (++idx === arr.length) 
      callback(); 
     else 
      crawlSite(idx, arr, page, callback); 
    }); 
} 

function startPhantom(cb) { 
    phantom.create(function(ph) { 
     phInstance = ph; 
     ph.createPage(function(page) { 
      phPage = page; 
      cb(ph, page); 
     }); 
    }); 
} 

function crawlUrl(path, page, cb) { 
    uri = url.resolve(baseUrl, path); 

    page.open(uri, function(status) { 
     var evaluateCb = function(result) { 
      result.path = path; 
      cb(result); 
     }; 
     //Timeout 2000ms seems pretty enough for majority ajax apps 
     setTimeout(function() { 
      if (status == 'success') 
       page.evaluate(function() { 
        var linkTags = document.querySelectorAll('a:not([rel="nofollow"])'); 
        var links = []; 
        for (var i = 0, ln; ln = linkTags[i]; i++) 
         links.push(ln.getAttribute('href')); 

        return { 
         'links': links, 
         'html': document.documentElement.outerHTML 
        }; 
       }, evaluateCb); 
     }, 2000); 
    }); 
} 

exports.crawlAll = function(callback) { 
    startPhantom(function(ph, page) { 
     crawlSite(0, ['/'], page, function() { 
      ph.exit(); 
      callback(); 
     }); 
    }); 
}; 

exports.crawlOne = function(path, callback) { 
    startPhantom(function(ph, page) { 
     crawlUrl(path, page, function(data) { 
      Snapshot.upsert(data); 
      ph.exit(); 
      callback(); 
     }); 
    }); 
}; 

當我運行這段代碼我確切的錯誤是:

phantom stderr: 'phantomjs' is not recognized as an internal or exte 
, 
operable program or batch file. 


assert.js:92 
    throw new assert.AssertionError({ 
     ^
AssertionError: abnormal phantomjs exit code: 1 
    at Console.assert (console.js:102:23) 
    at ChildProcess.<anonymous> (path to node modules\node_modules\phantom\phantom.js:150:28) 
    at ChildProcess.emit (events.js:98:17) 
    at Process.ChildProcess._handle.onexit (child_process.js:809:12) 

我的問題:這是最好的最簡單的方法去角逐與Facebook OG很好地發揮?如果是的話,任何人都可以確認他們是否設法讓這種技術能夠像上面所描述的那樣用幻影拋出斷言錯誤。

看起來這應該是一個比較普遍的工作,我很驚訝,我還沒有找到如何得到這個工作,一個漂亮的直線前進的教程,除非我只是還沒有適當照顧:■

感謝

+1

你的代碼在哪裏? – Tobi 2014-12-08 08:51:44

+0

感謝Tobi,編輯了上面的代碼。 – 2014-12-12 13:27:44

+0

哦,好吧,更多的看着這個(道歉我是新來的節點)看起來像幻影節點模塊只是節點和幻影之間的橋樑,我需要幻影作爲一個單獨的東西? – 2014-12-12 13:39:49

回答

1

好吧,

因爲我的問題基本上是「什麼是讓角度和節點與正確的頁面元至Facebook迴應的最好方式」。我現在可以發佈我的答案。

  1. 如上我指出認爲使用上述的方法phantom.js需要幻象被安裝並作爲node.js的服務器上的單獨進程中運行。 (任何人都可以確認或否認這一點?)

  2. 對於我的情況,我只是想讓用戶能夠從該網站發佈到Facebook和Facebook的鏈接使用開放圖元返回一個漂亮的鏈接。

考慮到這一點,我決定跳過上述教程解決方案中的phantom.js步驟。相反,當用戶點擊一個頁面時,我將一些代碼基本上保存在數據庫中。 HTML片段只包含我需要的Facebook元標記。然後,我使用上述教程的最後一部分將Facebook機器人指向我保存的HTML片段。

它似乎工作得很好。