Search code examples
javascriptnode.jsnode-fetch

Compare the HTML of 2 pages using fetch


I'm trying to compare 2 URLs, which should be identical, however for some reason I'm getting there's some difference. I'm wondering if there's something fundamental I'm misunderstanding with NPM fetch.

Here's my example code:

const fetch = require('node-fetch');

let startingPageBase = await fetch('https://www.example.com')
let startingPage = await startingPageBase.text()

let currentPageBase = await fetch('https://www.example.com')
let currentPage = await currentPageBase.text()

if (startingPageBase === currentPageBase) {
   console.log('Checked ')
} else {
   console.log('Not the same')
}

I'm basically trying to check if the HTML of the 2 pages is the same.


Solution

  • You are comparing the two promises, not the actual text values.

    do this:

    if (startingPage === currentPage) {
      console.log('Checked ')
    } else {
      console.log('Not the same')
    }