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.

Categories
JavaScript Answers

How to check Node Mongoose connection state without creating a new connection?

To check Node Mongoose connection state without creating a new connection, we get the connection property.

For instance, we write

require("./app.js");
const mongoose = require("mongoose");

console.log(mongoose.connection.readyState);

to get the mongoose.connect.readyState property to check if the Mongoose connection is ready.

Categories
JavaScript Answers

How to run multiple commands in package.json with JavaScript?

To run multiple commands in package.json with JavaScript, we use && to separate the commands.

For instance, in package.json, we write

{
  "scripts": {
    "init-assets": "webpack",
    "init-server": "node server/server.js",
    "start": "npm run init-assets && npm run init-server"
  }
}

to add the start script that runs the npm run init-assets and npm run init-server commands.

init-assets and init-server are the scripts added before start.

Categories
JavaScript Answers

How to fix ER_NOT_SUPPORTED_AUTH_MODE error on MySQL server?

To fix ER_NOT_SUPPORTED_AUTH_MODE error on MySQL server, we add the user.

For instance, we run

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'

to add the root user to the database so we can log in with the user in our apps.