有没有什么办法可以使jQuery与node.io工作?

Node.js noob。 我使用node.io来刮网站,但我很想在node.io中使用jQuery。 node.io提供的$对象没有提供太多的灵活性。

var nodeio = require('node.io'), options = {timeout: 10}, jQuery = require('jquery'); exports.job = new nodeio.Job(options, { input: ['hello', 'foobar', 'weather'], run: function (keyword) { this.getHtml('http://www.google.com/search?q=' + encodeURIComponent(keyword), function (err, $) { // SOMEHOW CREATE THE JQUERY OBJECT USING $ var results = $('#resultStats').text.toLowerCase(); this.emit(keyword + ' has ' + results); }); } }); 

谁能帮忙?

UPDATE

我没有注意到,node.io有一个选项使用jquery jsdom:true选项。 即使我使用这个选项,它也不起作用,我总是得到一个超时错误,或$是未定义的错误。

好的,找出问题所在。

node.io有一个使用jquery的选项,你需要传递这个选项jsdom:true 。 我使用的node.io(0.3.0)的版本有一个错误,它总是返回一个超时。

  // lib/node.io/dom.js window.onload = function() { callback.apply(self, [null, $, data, headers, response]); } 

现在在node.io 0.3.1版本中解决了这个问题

谢谢大家的回复!

看看jsdom – 一个在JS中模仿DOM的模块,允许您使用为浏览器devise的任何JS库。

这个博客文章解释了如何将它与jQuery结合: http : //blog.nodejitsu.com/jsdom-jquery-in-5-lines-on-nodejs 。

你可能只是看着使用mikeal的蜘蛛—它支持这个开箱即用的用例,并且是为了拼凑而build的。

https://github.com/mikeal/spider

例:

 var spider = require('../main'); spider() .route('www.nytimes.com', '/pages/dining/index.html', function (window, $) { $('a').spider(); }) .route('travel.nytimes.com', '*', function (window, $) { $('a').spider(); if (this.fromCache) return; var article = { title: $('nyt_headline').text(), articleBody: '', photos: [] } article.body = '' $('div.articleBody').each(function () { article.body += this.outerHTML; }) $('div#abColumn img').each(function () { var p = $(this).attr('src'); if (p.indexOf('ADS') === -1) { article.photos.push(p); } }) // console.log(article); }) .route('dinersjournal.blogs.nytimes.com', '*', function (window, $) { var article = {title: $('h1.entry-title').text()} // console.log($('div.entry-content').html()) }) .get('http://www.nytimes.com/pages/dining/index.html') .log('info') ; 

另外,如果你打算在node.io上 – 我认为node.io将数据作为可选parameter passing给它:

 io.getHTML('someurl', function(err, junk, data){ jsdom.env({ html: data, scripts : [ 'http://code.jquery.com/jquery-1.5.min.js' ] }, function(err, window) { var $ = window.jQuery; // use jquery here }); });