Categories
JavaScript Answers

How to make synchronous requests in Node.js?

To make synchronous requests in Node.js, we use the sync-request module.

For instance, we write

const request = require("sync-request");

//...

try {
  const res = request("GET", url);
} catch (e) {
  console.log(e);
}

to call request to make a get request to url.

We use the catch block to catch any request errors.

Categories
JavaScript Answers

How to fix TypeError: Converting circular structure to JSON in Node.js?

To fix TypeError: Converting circular structure to JSON in Node.js, we avoid stringifying the req object with JSON.stringify.

For instance, we write

console.log(req);

to call console.log to log the req object which has a circular structure.

Categories
JavaScript Answers

How to fix TypeError: firebase.storage is not a function with Node Firebase?

To fix TypeError: firebase.storage is not a function with Node Firebase, we import the firebase/storage module.

For instance, we write

import firebase from "firebase/app";
import "firebase/storage";

firebase.initializeApp({
  //...
});
const storageRef = firebase.storage().ref();

to import the firebase/storage module with

import "firebase/storage";

Then we can call firebase.storage.

Categories
JavaScript Answers

How to fix Node.js only processing six requests at a time?

To fix Node.js only processing six requests at a time, we can increase the max number of sockets.

To do this, we write

http.globalAgent.maxSockets = 20;

to increase the max number of simultaneous requests to 20.

Categories
JavaScript Answers

How to check in Node if module exists and if exists to load?

to check in Node if module exists and if exists to load, we add a try-catch block.

For instance, we write

try {
  const m = require("/home/test_node_project/per");
} catch (ex) {
  handleErr(ex);
}

to call require to require the /home/test_node_project/per module.

If the module can’t be included, then the catch block runs.