Can I use jQuery with Node.js?

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

提交回复
热议问题