Categories
JavaScript Answers

How to configure Axios to use SSL certificate with JavaScript?

To configure Axios to use SSL certificate with JavaScript, we call axios.get with a https agent object.

For instance, we write

const httpsAgent = new https.Agent({
  rejectUnauthorized: false,
  cert: fs.readFileSync("./usercert.pem"),
  key: fs.readFileSync("./key.pem"),
  passphrase: "YYY",
});

axios.get(url, { httpsAgent });

to create an https.Agent object with the cert certificate, key key file, and the passphrase.

Then we call axios.get with an object with the httpsAgent to use it to make secure requests.

We can also write

const httpsAgent = new https.Agent({
  rejectUnauthorized: false,
  cert: fs.readFileSync("./usercert.pem"),
  key: fs.readFileSync("./key.pem"),
  passphrase: "YYY",
});

const instance = axios.create({ httpsAgent });

to create a new Axios instance with axios.create.

Categories
JavaScript Answers

How to fix ‘Error: Couldn’t find preset “es2015” relative to directory “/Users/username”‘ with JavaScript?

To fix ‘Error: Couldn’t find preset "es2015" relative to directory "/Users/username"’ with JavaScript, we install the Babel preset package.

To install it, we run

npm install babel-cli babel-preset-es2015

to install the Babel packages to clear the error.

Categories
JavaScript Answers

How to create an empty file in Node.js?

To create an empty file in Node.js, we use the open method.

For instance, we write

const fs = require("fs");
fs.open(path, "wx", (err, fd) => {
  // handle error
  fs.close(fd, (err) => {
    // handle error
  });
});

to call open with the path to open the file at the path.

We create the file since we called it with the w permission.

And then we call close to close the file.

Categories
JavaScript Answers

How to fix the “Failed to compile. Module not found: Can’t resolve ‘react-router-dom'” error with JavaScript?

To fix the "Failed to compile. Module not found: Can’t resolve ‘react-router-dom’" error with JavaScript, we install the react-router-dom package.

To install it, we run

npm i react-router-dom

Then we install its type definitions with

npm i @types/react-router-dom
Categories
JavaScript Answers

How to scroll down until you can’t anymore with JavaScript Puppeteer?

To scroll down until you can’t anymore with JavaScript Puppeteer, we use a while loop.

For instance, we write

await page.evaluate(async () => {
  let scrollPosition = 0;
  let documentHeight = document.body.scrollHeight;

  while (documentHeight > scrollPosition) {
    window.scrollBy(0, documentHeight);
    await new Promise((resolve) => {
      setTimeout(resolve, 1000);
    });
    scrollPosition = documentHeight;
    documentHeight = document.body.scrollHeight;
  }
});

to call page.evaluate with a callback that uses a while loop to scroll down until the documentHeight is bigger than scrollPosition.

We call scrollBy to do the scrolling down the page.

When documentHeight is bigger than scrollPosition than we can’t scroll anymore.