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.

Categories
JavaScript Answers

How to fix fs.createWriteStream does not immediately create file in Node.js?

Sometimes, we want to fix fs.createWriteStream does not immediately create file in Node.js.

In this article, we’ll look at how to fix fs.createWriteStream does not immediately create file in Node.js.

How to fix fs.createWriteStream does not immediately create file in Node.js?

To fix fs.createWriteStream does not immediately create file in Node.js, we shold get the file from the 'open' event callback.

For instance, we write

const tempFile = fs.createWriteStream(tempFilePath);
tempFile.on("open", (fd) => {
  //...
});

to call fs.createWriteStream with the tempFilePath to create a write stream to write to the file at tempFilePath.

Then we call tempFile.on with 'open' to listen to the 'open' event which is triggered when the write stream is opened.

And then we can do whatever we want with the file inside.

Conclusion

To fix fs.createWriteStream does not immediately create file in Node.js, we shold get the file from the 'open' event callback.

Categories
JavaScript Answers

How to create a HTTP client request with a cookie with Node.js?

Sometimes, we want to create a HTTP client request with a cookie with Node.js.

In this article, we’ll look at how to create a HTTP client request with a cookie with Node.js.

How to create a HTTP client request with a cookie with Node.js?

To create a HTTP client request with a cookie with Node.js, we can use the http.request method with the cookie in the request headers.

For instance, we write

const options = {
  hostname: "example.com",
  path: "/somePath.php",
  method: "GET",
  headers: { Cookie: "myCookie=myvalue" },
};
let results = "";
const req = http.request(options, (res) => {
  res.on("data", (chunk) => {
    results += chunk;
    //...
  });
  res.on("end", () => {
    //...
  });
});

req.on("error", (e) => {
  //...
});

req.end();

to call http.request with the options object that has the headers property.

In it, we add the Cookie header which we set to the 'myCookie=myvalue' cookie string.

We get the response in the request callback and get the response chunks in the 'data' event listener which we call res.on with 'data' to listen to.

Conclusion

To create a HTTP client request with a cookie with Node.js, we can use the http.request method with the cookie in the request headers.

Categories
JavaScript Answers

How to use async package async.forEach with an object with Node.js?

Sometimes, we want to use async package async.forEach with an object with Node.js.

In this article, we’ll look at how to use async package async.forEach with an object with Node.js.

How to use async package async.forEach with an object with Node.js?

To use async package async.forEach with an object with Node.js, we can use the Object.entries method to return an array of key-value pair arrays from the object.

For instance, we write

async.forEach(
  Object.entries(dataObj),
  ([key, val], callback) => {
    console.log(key, val);
    callback();
  },
  (err) => {
    console.log("iterating done");
  }
);

to call async.forEach with the array of key-value pair arrays returned from Object.entries called with dataObj to iterate through the key-value pair array.

In the first callback, we get the key and val values from the key-value pair array being iterated through.

Then callback is called in the callback to do something with the entry.

The 2nd callback is run when iteration is done.

Conclusion

To use async package async.forEach with an object with Node.js, we can use the Object.entries method to return an array of key-value pair arrays from the object.