分类 NodeJs 下的文章

wget https://nodejs.org/dist/v16.16.0/node-v16.16.0-linux-x64.tar.xz


xz -d node-v16.16.0-linux-x64.tar.xz

tar xf node-v16.16.0-linux-x64.tar

mv node-v16.16.0-linux-x64 /usr/local/node-v16.16.0

ln -s /usr/local/node-v16.16.0/bin/node /usr/local/bin/node
ln -s /usr/local/node-v16.16.0/bin/npm /usr/local/bin/npm

vi /etc/profile


export NODEJS=/usr/local/node-v16.16.0
export PATH=$PATH:$NODEJS/bin

source /etc/profile

npm 的 redis库已经升级了,npm默认的是4版本,我用的默认安装,一直连接不上redis,只好指定安装3版本。
npm install redis@3.1.2

安装依赖

npm install jquery
npm install jsdom

示例

const fs = require('fs');
const { JSDOM } = require('jsdom');
const jquery = require('jquery');

function get(url, callback) {
    try {
        JSDOM.fromURL(url).then(jsenv => {
            const $ = jquery(jsenv.window);
            const id_html = $('#dom_id').html();
            callback(result);
        });
    } catch (e) {
        console.log(e);
    }
}
get('https://www.xxxx.com/xxx')

安装相关依赖

npm install jquery
npm install jsdom

引入方式

const jsdom = require('jsdom');
const {JSDOM} = jsdom;
const {document} = (new JSDOM('<!doctype html><html><body></body></html>')).window;
global.document = document;
const window = document.defaultView;
const $ = require('jquery')(window);