2015-05-19 102 views
-1

我在一個有趣的地方。我對Full-Stack相當陌生,所以我甚至不確定我想要做什麼是可能的......所以請耐心等待。我正在嘗試創建一個RSS聚合器,可以通過rss收集文章的內容並根據內容對其進行過濾。無論如何,我可以在後端應用程序中使用JQuery庫嗎?

我在一個JavaScript文件中使用ajax調用通過jQuery沒有連接到任何HTML頁面。它通過app.js稱爲

var GetRSS = require('./public/javascripts/GetRSS.js'); 

裏面的GetRSS文件:

$.ajax({ 
    type: "GET", 
    url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(url), 
    dataType: 'json', 
    error: function(){ 
     alert('Unable to load feed. Incorrect path or invalid feed.'); 
    }, 
    success: function(xml){ // Save items after successful read. They will eventually go into a database. 
     values = xml.responseData.feed.entries; 
     var art = new Article(values); 
     var pretty = JSON.stringify(values,null,2); // This is for printing purposes (when needed). 
     console.log("1"); 
     populateArticle(values); 
    } 

然而,當我起旋服務器,它收到以下錯誤:

$.ajax({ 
    ^
ReferenceError: $ is not defined 

我嘗試加入以下javascript:

var jQuery = require('./jquery.js'); 

但它沒有幫助。要進行迭代,此時我沒有HTML文件,因爲它只會從數據庫加載「GetRSS」文件始終運行和填充的內容。我在網上看到的任何地方都通過使用HTML中的腳本標籤將JQuery與JS文件聯繫起來。

是否有可能以我嘗試的方式使用JQuery庫?如果不是,另一種選擇是什麼?

+0

通過CDN包含jQuery:https://developers.google.com/speed/libraries/ – jmargolisvt

+1

@jmargolisvt爲什麼它會比從本地主機加載更好? – Barmar

+0

@Barmar可靠,簡單,只是想讓他去。 – jmargolisvt

回答

1

jQuery有一個npm package。您可以使用npm install --save jquery命令和require它在您的Node環境中安裝它。

請注意,您也可以使用cheerio代替jQuery進行DOM操作,並且由於Node環境中沒有XMLHttpRequest對象,因此您無法發送Ajax請求。對於HTTP請求,你可以使用request包:

var request = require('request'); 
request(url, function (error, response, body) { 
    if (!error && response.statusCode == 200) { 
     console.log(body); 
    } 
}); 
+0

謝謝!我使用請求,並能夠抓住rss就好了。我非常感謝幫助。然而,試圖將jquery作爲一個包包含在內仍然存在「$」引用錯誤。但我想我不得不離開服務器端。 – Dougyfresh

0

你不能只是一個js文件jQuery的運行。您需要製作一個.html頁面,並將它和您的GetRSS.js包含在您的文件中以對其進行測試。

例如:

<html> 
    <head> 
     <script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js'></script> 
     <script type='text/javascript' src='/public/javascripts/GetRSS.js'></script> 
    </head> 
    <body onload="GetRSS()"> 
    </body> 
</html> 

修改GetRSS.js:

function GetRSS() { 
    alert('GOT THE EXTERNAL RSS!'); 
    $.ajax({ 
     type: "GET", 
     url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(url), 
     dataType: 'json', 
     error: function(){ 
      alert('Unable to load feed. Incorrect path or invalid feed.'); 
     }, 
     success: function(xml){ // Save items after successful read. They will eventually go into a database. 
      values = xml.responseData.feed.entries; 
      var art = new Article(values); 
      var pretty = JSON.stringify(values,null,2); // This is for printing purposes (when needed). 
      console.log("1"); 
      alert('FOUND ENTRY!'); 
      populateArticle(values); 
     } 
    }); 
} 

你應該再能沒有問題,運行代碼。

+0

但是他怎麼能在node.js中加載這個HTML頁面呢? – Barmar

+0

同樣的概念適用於節點。當你啓動你的app.js時,你需要先調用jquery,然後再調用GetRSS。如果你想使用jQuery服務器端,那麼你將需要下載類似nodeQuery的東西。 –

+0

GHETTO.CHiLD,我沒有任何HTML頁面。這完全是一個節點應用程序。我會研究nodeQuery,這聽起來像我正在尋找的。 – Dougyfresh

相關問題