Nodejs jQuery needs jsdom

后端 未结 5 1090
执笔经年
执笔经年 2021-01-12 17:13
$.getJSON(\'https://api.twitch.tv/kraken/channels/\' + SLoppierKitty7, function(channel) {

if (channel[\"stream\"] == null) { 
    var live =\"no\"

} else {

              


        
5条回答
  •  失恋的感觉
    2021-01-12 18:00

    Yes, it does. jQuery expects the 'window' to be there, which is normally a browser-only object. As such, the window needs to be simulated. That is what jsdom does.

    After including jsdom (npm install jsdom):

    // Load jsdom, and create a window.
    var jsdom = require("jsdom").jsdom;
    var doc = jsdom();
    var window = doc.defaultView;
    
    // Load jQuery with the simulated jsdom window.
    $ = require('jquery')(window);
    

    See the jsdom documentation for more information: https://www.npmjs.com/package/jsdom

提交回复
热议问题