Categories
JavaScript Answers

How to use the DOMParser with Node.js?

Sometimes, we want to use the DOMParser with Node.js.

In this article, we’ll look at how to use the DOMParser with Node.js.

How to use the DOMParser with Node.js?

To use the DOMParser with Node.js, we can use the Cheerio library.

We install it by running

npm i cheerio

Then, we write

const cheerio = require('cheerio');

const $ = cheerio.load(`<!DOCTYPE html><p>Hello world</p>`);
$('p').text('Bye moon');
$.html();

to call cheerio.load with an HTML string to load the HTML string into the $ DOM object.

Then we select p elements and call text to set the text of the p element.

And we call $.html to return the HTML of the manipulate document.

Conclusion

To use the DOMParser with Node.js, we can use the Cheerio library.

Categories
JavaScript Answers

How to parse a HTML page with Node.js?

Sometimes, we want to parse a HTML page with Node.js.

In this article, we’ll look at how to parse a HTML page with Node.js.

How to parse a HTML page with Node.js?

To parse a HTML page with Node.js, we can use the Cheerio library.

To install it, we run

npm i cheerio

Then we write

const cheerio = require('cheerio');
const $ = cheerio.load('<h2 class="title">Hello world</h2>');

$('h2.title').text('Hello there!');
$('h2').addClass('welcome');

$.html();

to call cheerio.load to load the HTML string into the $ object.

Then we call $ with the CSS selector string to select the elements we want to manipulate.

And we call text to set the text of the h2 element with class title.

And we call addClass to add the welcome class to all h2 elements.

We return the HTML of the manipulated document with $.html.

To get the HTML string from a web page, we can make a request with any HTTP client.

Conclusion

To parse a HTML page with Node.js, we can use the Cheerio library.

Categories
JavaScript Answers

How to wait N seconds before continuing to the next line with Puppeteer?

Sometimes, we want to wait N seconds before continuing to the next line with Puppeteer.

In this article, we’ll look at how to wait N seconds before continuing to the next line with Puppeteer.

How to wait N seconds before continuing to the next line with Puppeteer?

To wait N seconds before continuing to the next line with Puppeteer, we can use the waitForTimeout method.

For instance, we write

await page.waitForTimeout(1000);
await frame.waitForTimeout(1000);

to call waitForTimeout to pause script execution on the page or frame for 1 second.

Conclusion

To wait N seconds before continuing to the next line with Puppeteer, we can use the waitForTimeout method.

Categories
JavaScript Answers

How to convert string to buffer with Node.js?

Sometimes, we want to convert string to buffer with Node.js.

In this article, we’ll look at how to convert string to buffer with Node.js.

How to convert string to buffer with Node.js?

To convert string to buffer with Node.js, we can use the Buffer.from method.

For instance, we write

const buf = Buffer.from(bufStr, 'utf8');

to call Buffer.from with the bufStr string and the encoding of the string to return a buffer created from the string.

Conclusion

To convert string to buffer with Node.js, we can use the Buffer.from method.

Categories
JavaScript Answers

How to get the domain originating the request in Express.js?

Sometimes, we want to get the domain originating the request in Express.js.

In this article, we’ll look at how to get the domain originating the request in Express.js.

How to get the domain originating the request in Express.js?

To get the domain originating the request in Express.js, we can use req.get with the 'host' or 'origin' header string.

For instance, we write

const host = req.get('host');

to call req.get with 'host' to get the hostname of the client that made the request.

For getting the host of cross domain requests, we write

const origin = req.get('origin');

to get the hostname of a cross origin request by calling req.get with 'origin'.

Conclusion

To get the domain originating the request in Express.js, we can use req.get with the 'host' or 'origin' header string.