Categories
JavaScript Answers

How to fix Node.js request CERT_HAS_EXPIRED error?

To fix Node.js request CERT_HAS_EXPIRED error, we set the rejectUnauthorized property to false.

For instance, we write

const request = require("request");

const agentOptions = {
  host: "www.example.com",
  port: "443",
  path: "/",
  rejectUnauthorized: false,
};
const agent = new https.Agent(agentOptions);

request(
  {
    url: "https://www.example.com/api/endpoint",
    method: "GET",
    agent,
  },
  (err, resp, body) => {
    // ...
  }
);

to call request with the agent object we by calling Agent with the agentOptions to make the request.

We set rejectUnauthorized to skip checking for invalid certificate.

Categories
JavaScript Answers

How to write formatted JSON in Node.js?

To write formatted JSON in Node.js, we call JSON.stringify.

For instance, we write

const formatted = JSON.stringify(object, null, 4);

to call JSON.stringify to return a JSON stringify with the object object converted to a JSON string.

We call it with 4 as the 3rd argument to indent each level with 4 spaces.

Categories
JavaScript Answers

How to set npm credentials using `npm login` without reading from stdin with JavaScript?

To set npm credentials using npm login without reading from stdin with JavaScript, we run npm set.

For instance, we run

npm set //<registry>/:_authToken $TOKEN

to run npm set to set the authToken to the $TOKEN environment variable value.

Categories
JavaScript Answers

How to convert HTML to PDF with Node.js?

To convert HTML to PDF with Node.js, we use Puppeteer.

For instance, we write

const puppeteer = require("puppeteer");
const handlebars = require("handlebars");

module.exports.htmlToPdf = async ({ templateHtml, dataBinding, options }) => {
  const template = handlebars.compile(templateHtml);
  const finalHtml = encodeURIComponent(template(dataBinding));

  const browser = await puppeteer.launch({
    args: ["--no-sandbox"],
    headless: true,
  });
  const page = await browser.newPage();
  await page.goto(`data:text/html;charset=UTF-8,${finalHtml}`, {
    waitUntil: "networkidle0",
  });
  await page.pdf(options);
  await browser.close();
};

to define the htmlToPdf function.

In it, we call launch to start a browser.

We call page.goto to go to the URL of the page we want to convert to a PDF.

Next we call pdf to convert it to a PDF.

Categories
JavaScript Answers

How to use MongoDB with promises in Node.js?

To use MongoDB with promises in Node.js, we use async and await.

For instance, we write

const main = async () => {
  let client, db;
  try {
    client = await MongoClient.connect(mongoUrl, { useNewUrlParser: true });
    db = client.db(dbName);
    const dCollection = db.collection("collectionName");
    const result = await dCollection.find();
    return result.toArray();
  } catch (err) {
    console.error(err);
  } finally {
    client.close();
  }
};

to call connect to connect to the Mongo database.

We call db to get the database.

We call collection to get the collection.

And we call find to return a promise with the collection data.

We call toArray to convert the data to an array.