| Quaintous | Published: |
(tags)
|
(category)
|
|
| Updated: | ||||
| The Mystery of jQuery in Nodejs | ||||
Its majesty, the jQueryThere is most probably no web developer who isn’t aware of jQuery: it provides a set of cross-browser wrappers and helper functions which are built upon functions and objects provided by the global context (e.g. But what does it really mean when jQuery is used within a Javascript runtime, where the global context is different from that of browsers and does not provide a DOM object (i.e. Node.js vs. BrowserThe release of Node.js was for some equal to being able to run the code which they had already written for client-side execution in the browser on the server-side. This lead to a lot of confusion, which in turn lead to the genesis of this article. The difference between Node.js and a browser in terms of Javascript runtime is indicated (in a subtle manner) in the definition of Node, stating that it is a
it simply means that it allows you to execute javascript code. Browsers also have their own JS runtime to execute scripts on the client side and in addition provide a mean “for representing and interacting with objects in HTML, XHTML, and XML documents.” and that is the Document Object Model (DOM). Another important difference is the module system of Node.js which allows to dynamically import Javascript code: It can be seen that actually the code which is meant to run in Node environment will most probably not run in the Browser and vice versa. There are, however, tools (e.g. browserify) which make it possible (not in all cases) for Node code to run also in the browser, but that is not the topic of this article. jQuery and Node.jsjQuery’s claim is to
To understand the mismatch of Node.js and jQuery, lets start with an example, which was also a question on stackoverflow:
First of all it should be noted that for CommonJS and all other environments (such as Node) which support the module system, jQuery has to be initiated with a for example using jsdom (version In other words if you want to use jQuery inside Node you should have a DOM object already prepared and use it to initialize jQuery. Afterwards you can make use of selectors, etc. What now?Now is time to think out of the box, to go through five stages of grief ASAP and accept that jQuery is not the best for server-side solutions. Asides from requiring to have access to an actual DOM object, it is delivered with loads of other functionalities which are of no use in Node.js environment. There are definitely better solutions! In one of my latest projects I came across cheerio and got to appreciate it very quickly. My goal was to fetch an HTML page, use CSS selectors to select rows of a specific table and to extract required information. As it turned out, the solution was pretty straight forward! The following snippet fetches the main page of this site and lists the (relative) links to the latest articles: |
||||
| License: | ![]() |
🟢 No JavaScript | (Notes) | |
| 🟢 No Tracking | ||||
