Skip to content Skip to sidebar Skip to footer

How Can I Access Dom Elements Using Node Js?

JAVASCRIPT FILE const path = require('path'); const http = require('http'); const fs = require('fs'); const dir = '../frontend/'; const server = http.createServer((request, respo

Solution 1:

The error above is very explicit:

document is not defined

document is not exist in Node (e.g. server) environment, it's exist only in browser.

You will need to use separate library to parse your HTML file in order to do queries like querySelector('.container') and etc.

Libraries to take a look (as an example):

https://github.com/taoqf/node-fast-html-parser

https://github.com/cheeriojs/cheerio

Solution 2:

In NodeJS, the document Object doesn't exist. You can't access DOM-Nodes or anything in the DOM in NodeJS.

NodeJS is serverside Javascript.

If you want any data from the client, you have to add client-side code and send the data from the client to the server. This is done with a post-request. In the Backend, you have to setup a server with routes. To setup a server in NodeJS, use Express (frequently used). Google about it.

Solution 3:

Node.js is server side, the document doesn't exist. So the Dom doesn't exist. You need to try with client web browser side. I mean simply just create a web page document

But you can also check this answer for more information

stackoverflow.com/questions/34309557/how-to-access-dom-using-node-js

Post a Comment for "How Can I Access Dom Elements Using Node Js?"