Can I use jQuery with Node.js?

前端 未结 20 1536
清歌不尽
清歌不尽 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:03

    My working code is:

    npm install jquery
    

    and then:

    global.jQuery   = require('jquery');
    global.$        = global.jQuery;
    

    or if the window is present, then:

    typeof window !== "undefined" ? window : this;
    window.jQuery   = require('jquery');
    window.$        = window.jQuery;
    
    0 讨论(0)
  • 2020-11-22 06:05

    You have to get the window using the new JSDOM API.

    const jsdom = require("jsdom");
    const { window } = new jsdom.JSDOM(`...`);
    var $ = require("jquery")(window);
    
    0 讨论(0)
  • 2020-11-22 06:08

    WARNING

    This solution, as mentioned by Golo Roden is not correct. It is just a quick fix to help people to have their actual jQuery code running using a Node app structure, but it's not Node philosophy because the jQuery is still running on the client side instead of on the server side. I'm sorry for giving a wrong answer.


    You can also render Jade with node and put your jQuery code inside. Here is the code of the jade file:

    !!! 5
    html(lang="en")
      head
        title Holamundo!
        script(type='text/javascript', src='http://code.jquery.com/jquery-1.9.1.js')
      body
        h1#headTitle Hello, World
        p#content This is an example of Jade.
        script
          $('#headTitle').click(function() {
            $(this).hide();
          });
          $('#content').click(function() {
            $(this).hide();
          });
    
    0 讨论(0)
  • 2020-11-22 06:10

    jQuery module can be installed using:

    npm install jquery
    

    Example:

    var $ = require('jquery');
    var http = require('http');
    
    var options = {
        host: 'jquery.com',
        port: 80,
        path: '/'
    };
    
    var html = '';
    http.get(options, function(res) {
    res.on('data', function(data) {
        // collect the data chunks to the variable named "html"
        html += data;
    }).on('end', function() {
        // the whole of webpage data has been collected. parsing time!
        var title = $(html).find('title').text();
        console.log(title);
     });
    });
    

    References of jQuery in Node.js** :

    • http://quaintous.com/2015/07/31/jqery-node-mystery/
    • http://www.hacksparrow.com/jquery-with-node-js.html
    0 讨论(0)
  • 2020-11-22 06:10

    The module jsdom is a great tool. But if you want to evaluate entire pages and do some funky stuff on them server side I suggest running them in their own context:

    vm.runInContext
    

    So things like require / CommonJS on site will not blow your Node process itself.

    You can find documentation here. Cheers!

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

    You can use Electron, it allows hybrid browserjs and nodejs.

    Before, I tried to use canvas2d in nodejs, but finally I gave up. It's not supported by nodejs default, and too hard to install it (many many ... dependeces). Until I use Electron, I can easily use all my previous browserjs code, even WebGL, and pass the result value(eg. result base64 image data) to nodejs code.

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