Can I use jQuery with Node.js?

前端 未结 20 1628
清歌不尽
清歌不尽 2020-11-22 05:42

Is it possible to use jQuery selectors/DOM manipulation on the server-side using Node.js?

相关标签:
20条回答
  • 2020-11-22 06:19

    Yes you can, using a library I created called nodeQuery

    var Express = require('express')
        , dnode = require('dnode')
        , nQuery = require('nodeQuery')
        , express = Express.createServer();
    
    var app = function ($) {
        $.on('ready', function () {
            // do some stuff to the dom in real-time
            $('body').append('Hello World');
            $('body').append('<input type="text" />');
            $('input').live('click', function () {
                console.log('input clicked');
                // ...
            });
        });
    };
    
    nQuery
        .use(app);
    
    express
        .use(nQuery.middleware)
        .use(Express.static(__dirname + '/public'))
        .listen(3000);
    
    dnode(nQuery.middleware).listen(express);
    
    0 讨论(0)
  • 2020-11-22 06:19

    None of these solutions has helped me in my Electron App.

    My solution (workaround):

    npm install jquery
    

    In your index.js file:

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

    In your .js files write yours jQuery functions in this way:

    jQuery(document).ready(function() {
    
    0 讨论(0)
  • 2020-11-22 06:19

    No. It's going to be quite a big effort to port a browser environment to node.

    Another approach, that I'm currently investigating for unit testing, is to create "Mock" version of jQuery that provides callbacks whenever a selector is called.

    This way you could unit test your jQuery plugins without actually having a DOM. You'll still have to test in real browsers to see if your code works in the wild, but if you discover browser specific issues, you can easily "mock" those in your unit tests as well.

    I'll push something to github.com/felixge once it's ready to show.

    0 讨论(0)
  • 2020-11-22 06:20

    As of jsdom v10, .env() function is deprecated. I did it like below after trying a lot of things to require jquery:

    var jsdom = require('jsdom');
    const { JSDOM } = jsdom;
    const { window } = new JSDOM();
    const { document } = (new JSDOM('')).window;
    global.document = document;
    
    var $ = jQuery = require('jquery')(window);

    Hope this helps you or anyone who has been facing these types of issues.

    0 讨论(0)
  • 2020-11-22 06:21

    npm install jquery --save #note ALL LOWERCASE

    npm install jsdom --save

    const jsdom = require("jsdom");
    const dom = new jsdom.JSDOM(`<!DOCTYPE html>`);
    var $ = require("jquery")(dom.window);
    
    
    $.getJSON('https://api.github.com/users/nhambayi',function(data) {
      console.log(data);
    });
    
    0 讨论(0)
  • 2020-11-22 06:22

    At the time of writing there also is the maintained Cheerio.

    Fast, flexible, and lean implementation of core jQuery designed specifically for the server.

    0 讨论(0)
提交回复
热议问题