-1

I'm in an interesting spot. I'm fairly new to Full-Stack, so I'm not even sure what I am trying to do is possible...so bear with me. I'm trying to create an RSS aggregator that can collect the contents of the articles via rss and filter them based on content. Regardless,

I'm using an ajax call through JQuery in a javascript file that is not attached to any HTML page. It is called via app.js as

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

Inside the GetRSS file:

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

However, when I spin-up the server, it get the following error:

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

I try and include javascript by adding:

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

But it doesn't help. To iterate, I do not have an HTML file at this moment, as it will simply load content from the DB that is "GetRSS" file is always running and populating. Everywhere I've looked online ties JQuery to the JS file by using script tags in HTML.

Is it possible to utilize the JQuery library in the manner I am trying? If not, what's another option?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Dougyfresh
  • 586
  • 3
  • 15

2 Answers2

1

jQuery has a npm package. You can install it using npm install --save jquery command and require it in your Node environment.

Note that you can also use cheerio instead of jQuery for DOM manipulations and since there is no XMLHttpRequest object on the Node environment you can't send an Ajax request. For making http requests you can use the request package:

var request = require('request');
request(url, function (error, response, body) {
  if (!error && response.statusCode == 200) {
      console.log(body);
  }
});
Ram
  • 143,282
  • 16
  • 168
  • 197
  • Thanks! I used request and was able to grab the rss just fine. I really appreciate the help. However, trying to include jquery as a package still had "$" have a reference error. But I guess I'll have to go without it for server-side. – Dougyfresh May 19 '15 at 02:57
0

you cannot run jquery from just a .js file. you will need to make a .html page and include it and your GetRSS.js in your file to test it.

example:

<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>

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

you should then be able to run your code without issue.

GHETTO.CHiLD
  • 3,267
  • 1
  • 22
  • 34
  • But how can he load that HTML page in node.js? – Barmar May 19 '15 at 02:41
  • the same concept applies for node. when you start your app.js you need to call jquery first then your GetRSS. If you want to use jquery server side then you will need to download something like nodeQuery. – GHETTO.CHiLD May 19 '15 at 11:55
  • GHETTO.CHiLD, I don't have any HTML page. This is entirely a node app. I'll look into nodeQuery, that sounds like what I am looking for. – Dougyfresh May 19 '15 at 15:31