Send and Receive Data from a Node.js Script in Bash using the Process Object

Cameron Nokes
InstructorCameron Nokes
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 3 years ago

Bash is great, but sometimes we need the power of a more robust programming language to accomplish our goals. In this lesson, we’ll look at how we can integrate node.js within bash to send data to and from a node.js script. First we’ll look at how to do quick one-liners at the shell using node's -p (print) option. Then we'll write a node.js script that accepts parameters from the shell to check the value of a query string. In node.js, we can use the process object to interact with command line arguments and stdio.

Instructor: [00:00] The simplest way to get data from a Node script is to invoke Node using the P option. P stands for print. Here, we pass the string. Let's see how many CPUs this system has.

[00:12] Note that we don't have to require a core Node.js module like OS when it's invoked like this. We can store the result in a Bash variable easily as well, and then we'll echo CPUs.

[00:25] How do we send the command link parameters into a Node.js script? For example, let's say we want a script that can parse and check the value of a query string. Let's write a quick Node script with an API that looks like this.

[00:37] I'll call it QueryStringCheck, and then we'll pass a query string like this. Then, we'll tell it what property in here we want to check and then the expected value of it. Let's go ahead and implement that.

[00:48] I have my QS check file here. Note that I've already given it execute permissions. You have to do that in order to invoke it directly. First of all, I add a Node shebang at the top. This tells the shell to use the Node.js binary to invoke this script.

[01:04] Let's get the parameters passed to the script. All of the script's parameters are stored in the process that [inaudible] . What we're going to do is we're going to slice off the first two elements of it.

[01:16] The first element is always the path to the Node binary, and the second element is the file path to this script. We don't need those, so I sliced them off. Then in here is where we're going to have the parameters that we're interested in. I have the query string that the user passed, their query string property that they want checked, and then the expected value for that property.

[01:39] First off, down here we'll do a little bit of input validation. We'll say that all of these parameters have to be defined. If they are empty, we'll throw an error. First, we'll write to standard error, and then we'll exit with a 1 status.

[01:55] Let's get the actually value. For that, I'll need the parse meth in from the query string module. I'll run parse on the query string, and then we'll grab the prop that the user passed. Then, let's do the actual comparison. We'll say if actual there is equal to the expected.

[02:18] We'll write that out and we'll exit with a 0status. If they're not equal, we'll write to standard error and say they're not equal and exit with a 1 or error status. Let's run this and try it out. Invoke our scripts. Let's check the exit code. It's a 0Let's change this so it's not true and check the exit status. It's a 1. Cool, looks like it's working.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today