Categories
JavaScript Answers

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

Sometimes, we want to add custom certificate authority (CA) to Node.js.

In this article, we’ll look at how to add custom certificate authority (CA) to Node.js.

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

To add custom certificate authority (CA) to Node.js, we can call https.globalAgent.options.ca.push with the certificate file.

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));
}

We loop through the path strings in trustedCa with a for-of loop.

Then we call https.globalAgent.options.ca.push to push the certificate file that we read into memory with fs.readFileSync.

We set https.globalAgent.options.ca to an array before calling push so its value is always an array.

Conclusion

To add custom certificate authority (CA) to Node.js, we can call https.globalAgent.options.ca.push with the certificate file.

Categories
JavaScript Answers

How to get the object ID after an object is saved in Mongoose?

Sometimes, we want to get the object ID after an object is saved in Mongoose.

In this article, we’ll look at how to get the object ID after an object is saved in Mongoose.

How to get the object ID after an object is saved in Mongoose?

To get the object ID after an object is saved in Mongoose, we can get the value from the callback that’s run after we call save.

For instance, we write

const {
  Schema
} = require('mongoose')

mongoose.connect('mongodb://localhost/lol', (err) => {
  if (err) {
    console.log(err)
  }
});

const ChatSchema = new Schema({
  name: String
});

mongoose.model('Chat', ChatSchema);

const Chat = mongoose.model('Chat');

const n = new Chat();
n.name = "chat room";
n.save((err, room) => {
  console.log(room._id);
});

to create a schema with the Schema constructor.

Next, we register that Chat model with mongoose.model.

Then create a new document with the Chat model.

Finally, we call save with the saved document data set as the value of room.

And we get the object ID of the saved document from room._id.

Conclusion

To get the object ID after an object is saved in Mongoose, we can get the value from the callback that’s run after we call save.

Categories
JavaScript Answers

How to parse query string in Node.js?

Sometimes, we want to parse query string in Node.js.

In this article, we’ll look at how to parse query string in Node.js.

How to parse query string in Node.js?

To parse query string in Node.js, we can use the url module.

For instance, we write

const http = require('http');
const url = require('url');

const server = http.createServer((request, response) => {
  const {
    query: queryData
  } = url.parse(request.url, true);
  response.writeHead(200, {
    "Content-Type": "text/plain"
  });

  if (queryData.name) {
    response.end(queryData.name);
  } else {
    response.end("Hello World");
  }
});

server.listen(8000);

to call url.parse to parse the request.url value in the browser.

And we get the parsed query string values from the query property from the returned object.

Then we get the name query parameter value from queryData.name.

Conclusion

To parse query string in Node.js, we can use the url module.

Categories
JavaScript Answers

How to join tests from multiple files with Mocha.js?

Sometimes, we want to join tests from multiple files with Mocha.js.

In this article, we’ll look at how to join tests from multiple files with Mocha.js.

How to join tests from multiple files with Mocha.js?

To join tests from multiple files with Mocha.js, we can require the test file in another test.

For instance, we write

const importTest = (name, path) => {
  describe(name, () => {
    require(path);
  });
}

const common = require("./common");

describe("top", () => {
  beforeEach(() => {
    console.log("running something before each test");
  });
  importTest("a", './a/a');
  importTest("b", './b/b');
  after(() => {
    console.log("after all tests");
  });
});

to define the importTest which creates a test with describe to create a new test suite.

And we call require with path to run the tests at the given path.

Next,. we call importTest with the name of the test and path to the tests in the 2nd describe to run the tests in those files.

Conclusion

To join tests from multiple files with Mocha.js, we can require the test file in another test.

Categories
JavaScript Answers

How to run Mocha tests with extra options or parameters?

Sometimes, we want to run Mocha tests with extra options or parameters.

In this article, we’ll look at how to run Mocha tests with extra options or parameters.

How to run Mocha tests with extra options or parameters?

To run Mocha tests with extra options or parameters, we can set some environment variables before running the tests and then access them from process.env.

For instance, we run the tests with

env KEY=YOUR_KEY mocha test/*.js 

Then in the tests, we get the KEY environment variable with

const key = process.env.KEY;

Conclusion

To run Mocha tests with extra options or parameters, we can set some environment variables before running the tests and then access them from process.env.