Daniel Kariuki
Daniel Kariuki

Reputation: 21

How to check if two files have the same content

I am working with a nodejs application. I am querying an API endpoint, and storing the retrieved data inside a database. Everything is working well. However, there are instances where some data is not pushed to the database. In this case what I normally do is manually query the endpoint by assigning the application the date when that data was lost, and retrieve it since its stored in a server which automatically deletes the data after 2 days. The API and database fields are identical.

The following is not the problem, but to give you context, I would like to automate this process by making the application retrieve all the data for the past 48 HRS, save it in a .txt file inside the app. I will do the same, query my mssql database to retrieve the data for the past 48 hrs.

My question is, how can check whether the contents of my api.txt file are the same with that of the db.txt?

Upvotes: 0

Views: 550

Answers (1)

BiOS
BiOS

Reputation: 2304

You could make use of buf.equals(), as detailed in the docs

const fs = require('fs');

var api = fs.readFileSync('api.txt');
var db = fs.readFileSync('db.txt');

//Returns bool
api.equals(db)

So that:

if (api.equals(db))
   console.log("equal")
else
   console.log("not equal")

Upvotes: 1

Related Questions