Categories
JavaScript Answers

How to add custom certificate authority (CA) to Node.js?

To add custom certificate authority (CA) to Node.js, we call the options.ca.push method.

For instance, we write

const trustedCa = [
  "/etc/pki/tls/certs/ca-bundle.crt",
  "/path/to/custom/cert.crt",
];

https.globalAgent.options.ca = [];
for (const ca of trustedCa) {
  https.globalAgent.options.ca.push(fs.readFileSync(ca));
}

to loop through each trusrtedCa path with a for-of loop.

In it, we call https.globalAgent.options.ca.push with the file read from readFileSync to add the certificate file into the array with push.

Categories
JavaScript Answers

How to create Mongoose schema with array of Object IDs with JavaScript?

To create Mongoose schema with array of Object IDs with JavaScript, we use an array.

For instance, we write

const userSchema = mongoose.Schema({
  email: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  name: {
    first: { type: String, required: true, trim: true },
    last: { type: String, required: true, trim: true },
  },
  phone: Number,
  lists: [listSchema],
  friends: [{ type: ObjectId, ref: "User" }],
  accessToken: { type: String },
});
exports.User = mongoose.model("User", userSchema);

to create the userSchema that has the friends field set to an array of object IDs.

We do that by setting it an array with an object with type set toi ObjectId.

ref is the collection that the IDs are referencing.

Categories
JavaScript Answers

How to call an asynchronous function within map with JavaScript?

To call an asynchronous function within map with JavaScript, we use the for-of loop.

For instance, we write

const promises = teachers.map(async (m) => {
  return await request.get("...");
});

for (let val of promises) {
  //....
}

to call teachers.map with a callback to return an array of promises.

Then we use the for-of loop to loop through each promise in the promises array and run them.

Categories
JavaScript Answers

How to host multiple Node.js sites on the same IP/server with different domains?

To host multiple Node.js sites on the same IP/server with different domains, we call the listen method with the domain.

For instance, we write

const server = require("diet");

const app = server();
app.listen("http://example.com/");
app.get("/", ($) => {
  $.end("hello world ");
});

const sub = server();
sub.listen("http://subdomain.example.com/");
sub.get("/", ($) => {
  $.end("hello world at sub domain!");
});

const other = server();
other.listen("http://other.com/");
other.get("/", ($) => {
  $.end("hello world at other domain");
});

to create the server with the diet package.

Then we call listen with the domain or subdomain URL to list to request to.

We call get with each server to listen for get request from /.

Categories
JavaScript Answers

How to fix Node Version Manager (NVM) not recognized on Windows?

To fix Node Version Manager (NVM) not recognized on Windows?, we’ve to install it.

To install it, we download nvm from https://github.com/coreybutler/nvm-windows/releases.

Then we pick nvm-setupp.zip from the page.

We then unzip the file and click on the installer.

Finally, we run nvm to check if it’s installed.

Then we run nvm install 10 to install Node 10.

We run node -v to check the node version.

We check the installed versions with nvm list.

And we use nvm use 10 to use Node version 10.