Categories
JavaScript Answers

How to execute shell command with Node.js?

Sometimes, we want to execute shell command with Node.js.

In this article, we’ll look at how to execute shell command with Node.js.

How to execute shell command with Node.js?

To execute shell command with Node.js, we can use the child_process module’s spawn function.

For instance, we write

const runCmd = (cmd, args, callBack) => {
  const { spawn } = require("child_process");
  let child = spawn(cmd, args);
  let resp = "";

  child.stdout.on("data", (buffer) => {
    resp += buffer.toString();
  });
  child.stdout.on("end", () => {
    callBack(resp);
  });
};

runCmd("ls", ["-l"], (text) => {
  console.log(text);
});

to call the child_process module’s spawn function with the cmd command string and the args argument string array.

We call stdout.on to listen to the data event which has the output of the cmd command.

And then we call on again to listen to the end event which is emitted when the command finishes running.

We run callBack in the end event callback with the resp string.

Conclusion

To execute shell command with Node.js, we can use the child_process module’s spawn function.

Categories
JavaScript Answers

How to get day name from date with Moment.js and JavaScript?

Sometimes, we want to get day name from date with Moment.js and JavaScript.

In this article, we’ll look at how to get day name from date with Moment.js and JavaScript.

How to get day name from date with Moment.js and JavaScript?

To get day name from date with Moment.js and JavaScript, we can use the format method.

For instance, we write

const myDate = "2022-06-28T00:00:00";
const weekDayName = moment(myDate).format("dddd");
console.log(weekDayName);

to call moment with the myDate string to parse myDate to a moment object.

Then we call format with the 'dddd' string to return a string with the day name for the myDate date.

Conclusion

To get day name from date with Moment.js and JavaScript, we can use the format method.

Categories
JavaScript Answers

How to detect string which contains only spaces with JavaScript?

Sometimes, we want to detect string which contains only spaces with JavaScript.

In this article, we’ll look at how to detect string which contains only spaces with JavaScript.

How to detect string which contains only spaces with JavaScript?

To detect string which contains only spaces with JavaScript, we can use the string trim method.

For instance, we write

if (!str.trim()) {
  //...
}

to check if the string returned by str.trim is an empty string.

If it is, then the str string has only whitespaces in it.

Conclusion

To detect string which contains only spaces with JavaScript, we can use the string trim method.

Categories
JavaScript Answers

How to check if a cookie exists with JavaScript?

Sometimes, we want to check if a cookie exists with JavaScript.

In this article, we’ll look at how to check if a cookie exists with JavaScript.

How to check if a cookie exists with JavaScript?

To check if a cookie exists with JavaScript, we can use a regex.

For instance, we write

document.cookie.match(/^(.*;)?\s*MyCookie\s*=\s*[^;]+(.*)?$/);

to call match with a regex to look for the cookie with key MyCookie.

We make sure we’re looking for substring between a semicolon and with = after the key with (.*;)?\s and \s*[^;]+(.*)?.

document.cookie is a string with all the cookies.

Conclusion

To check if a cookie exists with JavaScript, we can use a regex.

Categories
JavaScript Answers

How to append to new line in Node.js?

Sometimes, we want to append to new line in Node.js.

In this article, we’ll look at how to append to new line in Node.js.

How to append to new line in Node.js?

To append to new line in Node.js, we can use the os.EOL constant.

For instance, we write

const os = require("os");

const processInput = (text) => {
  fs.open("H://log.txt", "a", 666, (e, id) => {
    fs.write(id, text + os.EOL, null, "utf8", () => {
      fs.close(id, () => {
        console.log("file is updated");
      });
    });
  });
};

to log the log.txt file with append permission with fs.open.

In the fs.open callback, we call fs.write to add the os.EOL end of line character after the text.

Then we call fs.close in the write callback to close the file.

Conclusion

To append to new line in Node.js, we can use the os.EOL constant.