从Node初始化jquery [英] Initializing jquery from Node

查看:82
本文介绍了从Node初始化jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下内容创建一个新项目:

I am creating a new project using the following:

$mkdir X
$cd X
$npm install jquery

然后创建一个新的app.js文件:

Then create a new app.js file:

var http = require('http');
var $ = require('jquery');
console.log("http="+ http);
console.log("$="+ $);
console.log("$.getJSON="+ $.getJSON);

输出为:

http=[object Object]
$=function ( w ) {...}
$.getJSON=undefined

为什么$ .getJSON是未定义的?使用最新的io.js v2.4.0.

Why is $.getJSON undefined ? Using latest io.js v2.4.0.

推荐答案

您正在尝试创建 <来自Node.js的c0> .这是行不通的,因为Node.js只是JavaScript运行时,并且与浏览器不同.

You are trying to create an XHR from within Node.js. This is not going to work since Node.js is simply a JavaScript runtime and is not the same as a browser.

如果您想通过HTTP协议从某处获取内容,则可以使用 request .例如(来自官方文档):

If you want to fetch something from somewhere over HTTP protocol, you can use something like request. For example (from official docs):

var request = require('request');
request('http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Show the HTML for the Google homepage. 
  }
})

您可以查看此答案(同样来自我),以获取有关结合使用jQuery和Node的更多信息. .js.

You can take a look at this answer (also from me) for more information on using jQuery in combination with Node.js.

再次更新[d!]:

所以您想知道jQuery节点模块如何区分浏览器和节点环境?当您在提供modulemodule.exports的CommonJS或类似环境中使用require jQuery时,得到的是工厂而不是实际的jQuery对象.如下所示,该工厂可用于创建jQuery对象,即使用 jsdom :

So you wanted to know how jQuery node module differentiates between browser and node environment? When you require jQuery inside a CommonJS or similar environment which provide module and module.exports, what you get is a factory and not the actual jQuery object. As you can see below, that factory can be used to create a jQuery object, i.e. using jsdom:

let jsdom = require("jsdom");
let $ = null;

jsdom.env(
  "http://quaintous.com/2015/07/31/jquery-node-mystery/",
  function (err, window) {
    $ = require('jQuery')(window);
  }
);

这是jQuery区分浏览器和io.js(或Node.js)的方式:

Here is the how jQuery differentiates between browser and io.js (or Node.js):

(function( global, factory ) {

    if ( typeof module === "object" && typeof module.exports === "object" ) {
        // For CommonJS and CommonJS-like environments where a proper `window`
        // is present, execute the factory and get jQuery.
        // For environments that do not have a `window` with a `document`
        // (such as Node.js), expose a factory as module.exports.
        // This accentuates the need for the creation of a real `window`.
        // e.g. var jQuery = require("jquery")(window);
        // See ticket #14549 for more info.
        module.exports = global.document ?
            factory( global, true ) :
            function( w ) {
                if ( !w.document ) {
                    throw new Error( "jQuery requires a window with a document" );
                }
                return factory( w );
            };
    } else {
        factory( global );
    }

// Pass this if window is not defined yet
}(typeof window !== "undefined" ? window : this, function( window, noGlobal ) {
  // implementation

  return jQuery;
}));

我将使用jQuery的npm软件包用于自定义版本,而不是与require一起使用!

I would use jQuery's npm package is meant for custom builds rather than to be used with require!

更新:

我觉得这个主题恰好使一些开发人员忙碌,所以我结合了自己的几个答案,并写了

I had the feeling that this subject happens to keep some devs busy, so I combined the couple of my own answers and wrote an article about the whole jQuery/Node combination!

这篇关于从Node初始化jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆