Play jQuery with Node.js

时间:2021-10-05 09:10:16

Motivation

Move the fucking browser interactions out of javascript development cycle, since Chrome is a monster eating memories.

Resources

jquery package
jsdom package

A tutorial: jsdom + jQuery in 5 lines with node.js

Setup

IDE

atom + atom-ternjs + script

node.js module

  npm install jquery
  npm install jsdom@3 // for v0.10.37

code

// see https://github.com/UncoolAJ86/node-jquery
//  see https://github.com/tmpvar/jsdom/tree/3.x
(function() {
  'use strict';

  var env = require('jsdom').env,
    html =
    '<html><body><h1>Hello World!</h1><p class="hello">Heya Big World!</body></html>';

  // first argument can be html string, filename, or url
  env(html, function(errors, window) {
    console.log(errors);

    var $ = require('jquery')(window);

    console.log($('.hello').text());
  });
}());

execute result

Play jQuery with Node.js