Home >Web Front-end >JS Tutorial >How Can I Use jQuery with Node.js on the Server-Side?

How Can I Use jQuery with Node.js on the Server-Side?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-18 09:35:02508browse

How Can I Use jQuery with Node.js on the Server-Side?

Server-Side jQuery with Node.js

It may not be immediately apparent, but Node.js can actually be used to run jQuery selectors and DOM manipulation functions.

Dependencies

To get started, install the necessary dependencies using npm:

npm install jquery jsdom

Integration

Once installed, integrate jQuery with Node.js using the following steps:

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

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

Usage

With jQuery now available in your Node.js environment, you can use it as you would on the client side. For instance, to select elements with a specific class:

$('body').find('.heading');

Note:

The jsdom dependency provides a JavaScript DOM implementation that is compatible with jQuery, allowing you to run jQuery code on the server.

The above is the detailed content of How Can I Use jQuery with Node.js on the Server-Side?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn