Categories
TypeScript Answers

How to create an object based on an interface file definition in TypeScript?

To create an object based on an interface file definition in TypeScript, we can create a class that extends the interface.

For instance, we write

interface IModal {
  content: string;
  form: string;
  //...
  foo: (bar: string) => void;
}

class Modal implements IModal {
  content: string;
  form: string;

  foo(param: string): void {}
}

to create the IModal interface with some fields.

Then we create the Modal class that has the fields listed in IModal.

Then we can use new Modal() to create a new Modal instance.

Conclusion

To create an object based on an interface file definition in TypeScript, we can create a class that extends the interface.

Categories
JavaScript Answers

How to query referenced objects in MongoDB?

Sometimes, we want to query referenced objects in MongoDB.

In this article, we’ll look at how to query referenced objects in MongoDB.

How to query referenced objects in MongoDB?

To query referenced objects in MongoDB, we can use the $lookup operator.

For instance, we write

db.Foo.aggregate(
  { $unwind: "$bars" },
  {
    $lookup: {
      from: "bar",
      localField: "bars",
      as: "bar",
    },
  },
  {
    $match: {
      "bar.testprop": true,
    },
  }
);

to call aggregate with the $lookup property set to an object with the referenced object properties we’re looking for.

We look for the collection specified with $lookup.from.

We look for the localField in the field specified in $lookup.from and we return the result in the field with the name set as the value of as.

We look for the field values in the $bars array since we set unwind to $bars.

And we search for the items with bar.testprop set to true with $match.

Conclusion

To query referenced objects in MongoDB, we can use the $lookup operator.

Categories
JavaScript Answers

How to encrypt data with a public key in Node.js?

Sometimes, we want to encrypt data with a public key in Node.js.

In this article, we’ll look at how to encrypt data with a public key in Node.js.

How to encrypt data with a public key in Node.js?

To encrypt data with a public key in Node.js, we can use the fs.readFileSync method to read the public key.

Then we use the crypt module to encrypt our data.

For instance, we write

const crypto = require("crypto");
const path = require("path");
const fs = require("fs");

const encryptStringWithRsaPublicKey = (
  toEncrypt,
  relativeOrAbsolutePathToPublicKey
) => {
  const absolutePath = path.resolve(relativeOrAbsolutePathToPublicKey);
  const publicKey = fs.readFileSync(absolutePath, "utf8");
  const buffer = Buffer.from(toEncrypt);
  const encrypted = crypto.publicEncrypt(publicKey, buffer);
  return encrypted.toString("base64");
};

const decryptStringWithRsaPrivateKey = (
  toDecrypt,
  relativeOrAbsolutePathtoPrivateKey
) => {
  const absolutePath = path.resolve(relativeOrAbsolutePathtoPrivateKey);
  const privateKey = fs.readFileSync(absolutePath, "utf8");
  const buffer = Buffer.from(toDecrypt, "base64");
  const decrypted = crypto.privateDecrypt(privateKey, buffer);
  return decrypted.toString("utf8");
};

to create the encryptStringWithRsaPublicKey and decryptStringWithRsaPrivateKey functions.

In encryptStringWithRsaPublicKey, we get the publicKey with fs.readFileSync to read the public key file.

Then we convert the toEncrypt string to a buffer with Buffer.from.

And then we call crypto.publicEncrypt to encrypt the buffer with the publicKey.

In decryptStringWithRsaPrivateKey, we read the privateKey with fs.readFileSync.

Then we get the buffer from the toDecrypt string with Buffer.from.

Next, we call crypto.privateDecrypt with privateKey and buffer to decrypt the buffer with the privateKey.

Finally, we convert the decrypted content to a string with toString.

Conclusion

To encrypt data with a public key in Node.js, we can use the fs.readFileSync method to read the public key.

Then we use the crypt module to encrypt our data.

Categories
JavaScript Answers

How to check if an environment variable is set in Node.js?

Sometimes, we want to check if an environment variable is set in Node.js.

In this article, we’ll look at how to check if an environment variable is set in Node.js.

How to check if an environment variable is set in Node.js?

To check if an environment variable is set in Node.js, we can check if the environment variable exists in process.env.

For instance, we write

if (process.env.MYKEY) {
  console.log("set");
} else {
  console.log("not set");
}

to check if the MYKEY environment variable is set by checking is process.env.MYKEY is truthy.

Conclusion

To check if an environment variable is set in Node.js, we can check if the environment variable exists in process.env.

Categories
JavaScript Answers

How to shut down an Express server gracefully when its process is killed with Node.js?

Sometimes, we want to shut down an Express server gracefully when its process is killed with Node.js.

In this article, we’ll look at how to shut down an Express server gracefully when its process is killed with Node.js.

How to shut down an Express server gracefully when its process is killed with Node.js?

To shut down an Express server gracefully when its process is killed with Node.js, we can use the @moebius/http-graceful-shutdown package.

To install it, we run

npm i @moebius/http-graceful-shutdown

Then we use it by writing

const express = require("express");
const { GracefulShutdownManager } = require("@moebius/http-graceful-shutdown");

const app = express();

const server = app.listen(8080);

const shutdownManager = new GracefulShutdownManager(server);

process.on("SIGTERM", () => {
  shutdownManager.terminate(() => {
    console.log("Server is gracefully terminated");
  });
});

We create a GracefulShutdownManager instance by calling the constructor with our server and assign it to shutdownManager.

And then we call shutdownManager.terminate to shut down our server gracefully when the SIGTERM signal is received.

Conclusion

To shut down an Express server gracefully when its process is killed with Node.js, we can use the @moebius/http-graceful-shutdown package.