0

Hi I am new here and hope that I can make myself clear. I've been trying to call(?) jQuery in node.js, but everything I do results a failure. I've been trying to solve this for hours and still stuck hopefully there is someone who can help me with this !

I am trying to fetch some info from twitch using their api. In this case if a streamer is online or offline. When I open the page localy ( without node.js) the jQuery code works fine. However when I try to use node.js it gives errors on this: $.

This is after some research and random trying.. (still new to node.js and jQuery):

` https://i.sstatic.net/rNC0z.jpg

`

Edit: The reason why I am trying to use node.js is to update my website without refreshing. So If someone goes online/offline, he/she should not be forced to refresh.

4
  • What version of JSDOM do you have installed? Commented May 25, 2017 at 19:50
  • 1
    Please, embed the image with you answer. External links are bad, because once the external resource is gone, the question becomes less valuable. Commented May 25, 2017 at 19:50
  • Since I am new here, there is a restriction on images. I will update the question later on when I get 10 reputation. jsdom version : "_from": "jsdom@latest", "_id": "[email protected]", "_inCache": true, "_location": "/jsdom", "_nodeVersion": "7.5.0", Commented May 25, 2017 at 19:59
  • 1
    @Smil3 You don't need an image to convey the information. Copy and paste the relevant text into the question. Commented May 25, 2017 at 20:14

2 Answers 2

2

"When the only tool you have is a hammer, every problem looks like a nail."

In this case, you're trying to use the wrong tool for the job. jQuery is built and optimized specifically for use in a browser environment. While you could force it to work by having a shadow DOM... don't.

If you wanted to use jQuery's selectors and DOM manipulation features, cheerio basically implements that API, but specifically for Node.js/server-side.

However, you sound like you want to use it's ajax() method (or one of it's helpers like post() or get().

Instead, you should use something built for Node. node-fetch (https://www.npmjs.com/package/node-fetch) is an excellent and easy to use option that was designed specifically for Node.js.

Here is a simple example from its documentation:

fetch('https://github.com/')
.then(function(res) {
    return res.text();
}).then(function(body) {
    console.log(body);
});

You first call fetch() with the URL you want. It'll return a Promise with a Response object. From that, you can extract the text() or anything else, and from there you can parse it.

If you need to pass options, call a different method (like POST), etc., you'll just give it an options object as the second parameter to the first call (check out their documentation).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer! Feels like I'm halfway there. I tried with node-fetch, however the problem I'm having right now is about sending the "client-id". Since I'm not sending the client-id, it's giving me HTTP 400 error. dev.twitch.tv/docs/v5/guides/using-the-twitch-api : "Requests can include both a client ID and an OAuth token. Requests without either one fail with an HTTP 400 error."
See, this is how to use fetch
@Smil3 Looks like there are two ways to send it. The easiest is just to add it to your URL: twitch.url?client_id=your_id_here. The other would be to send it as a header (that's what the -H is for cURL), which you can do by passing an object as the second parameter to fetch() with headers: fetch(url, { headers: { 'Client-ID': 'your_id_here' } }). If you need help beyond that, try to work it out on your own, and then post a new question with your updated work.
0

There is a npm module you can use that is based off jsdom.
It will give you a window object. Example:

var express = require('express');
var Window = require('window');
global.window = new Window();
global.document = window.document;
var $ = require('jquery');
var app = express();
function game() {
   $("body").append(`<h1>Hello world!</h1>`);
}
game();
app.get('/', function (req, res) {
    res.send(document.getElementsByTagName('html')[0].innerHTML)
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.