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
CSS

How to change the mouse cursor on mouse over to anchor-like style with CSS?

Sometimes, we want to change the mouse cursor on mouse over to anchor-like style with CSS.

In this article, we’ll look at how to change the mouse cursor on mouse over to anchor-like style with CSS.

How to change the mouse cursor on mouse over to anchor-like style with CSS?

To change the mouse cursor on mouse over to anchor-like style with CSS, we set the cursor style to pointer.

For instance, we write

#myDiv {
  cursor: pointer;
}

to set the cursor style of the element with ID myDiv to pointer so that when we move our mouse into the element, the pointer will show the hand icon that’s displayed for links by default.

Conclusion

To change the mouse cursor on mouse over to anchor-like style with CSS, we set the cursor style to pointer.

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.