Real-Time Web with Node.js Real-Time Web with Node.js

Accepting Input

Kyle Simpson
You Don't Know JS
Check out a free preview of the full Real-Time Web with Node.js course:
The "Accepting Input" Lesson is part of the full, Real-Time Web with Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Building on the Hello World example, Kyle modifies the Node application to accept command-line input. He then imports a module called Minimist that will help parse command-line variables.

Get Unlimited Access Now

Transcript from the "Accepting Input" Lesson

[00:00:00]
>> [MUSIC]

[00:00:04]
>> Kyle Simpson: We're not gonna deal too much about the input output thing from here on will do deal lightly with it. But what, I want to do is I want to go back to you can switch it back to console.log because that's a little bit easier a little bit more standard for us.

[00:00:18] But what if we wanted to except some form of input we didn't want to go all the way down trying to write our own readln loops or other things like that. But what if we wanted to accept some input from the command line via command line parameters? So for example, let's say we wanted to pull in the name to say hello to.

[00:00:39] So rather than printing out Hello World, I could take out the Hello part and I could say name. Now that's gonna be a need to be a variable that I declare and what is the value that I'm going to declare. So what if I wanted to pull the first argument off of the command line that was provided to my program and pull that in as my name.

[00:01:01] Again we go back to this process extension that's been provided to JavaScript, it's an automatic global and we can say argv. That's a very C style way of thinking about our arg variables, which is an array and the zero position is gonna be the node command. The one position is gonna be our one.js, so we want the two position, process.argv[2], that will be our name.

[00:01:28] Okay, now save that file, it's a complaining runner. [COUGH] All right, we save that file, we come back to our command line. We type in node 1.js, when we don't print anything. It's gonna say, Hello undefined because there is no argv[2]. But if we try our program again and we say world as that parameter, now it says Hello World.

[00:01:59] Some of you that have a little more experience with you may be entirely bored at this point you're like my god he's doing console.log status this is incredibly boring. I promise, we're building ourselves up, we wanna make sure that everybody feels confident with how the basic environment works.

[00:02:12] Most people teach you node by just throwing in a whole bunch of frameworks. The very first thing they do is teach you to drop in express or whatever. I think it's important to understand the actual mechanics of the environment or working with. So that's why we start at the beginning but I promise we'll get to more interesting stuff as we go along.

[00:02:30]
>> Kyle Simpson: Okay, so we've been able to pull in ourselves something from an argument. Now, there's all kinds of different ways that people use arguments on the command line. There's the standard UNIX style like and so forth and those sorts of things if I print it like that. Obviously, it's not doing any kind of parsing of what my command line is.

[00:02:53] Now you can manually do those things but that's not the kind of thing people wanna reinvent. So the first thing that we're gonna do is learn our first sort of helper module that we can pull in. And I'm gonna teach you about modules, so I'm gonna pull in something it's called.

[00:03:10] I just forgot the name of it, let me check real quick.
>> Kyle Simpson: Solutions/node_modules, what was it called? Minimist, I can't remember what it's called, all right, we're going to pull in. I'm going to call it args and I'm going to require minimist, now before you are able to run this program we're going to have to install it.

[00:03:37] Those of you that are here in person you might want to at this point if you don't have the node modules directory that I provided in the solutions folder. You might want to copy that over into your main folder so that you already have these installed. You can optionally do an npm install but if you're having network troubles, you can copy those over.

[00:03:56] If you're on the live stream, you're gonna, I'll show you the installer in a moment. What we require in this minimist module, and I'm kind of hand waving cuz it's kind of magical right now what that even means. But we're gonna require in a minimist project, what it does is it automatically will parse our arguments list for us.

[00:04:14] And pull out stuff like those parameter names that we're providing in those various different styles. So the minimist project when we require it returns us a function which we can immediately call. You kind of saw some of that before when we were returning functions that we could call.

[00:04:30] We could immediately call that and what we want to do is provide it our process.argv list. But let's go ahead and remove the first two parameters because nobody cares about the node in the one.js part. So we're going to slice off the first two parameters and provide just the process start on v.

[00:04:49] The other thing I'm going to do is I'm gonna provide an optional list to minimist which the only thing I really care about telling it to do is I wanna tell that in the string. That I want to treat the name property as a string parameter rather than as a boolean prime, okay.

[00:05:09] It won't make a terrible amount of sense why we're doing that right at this moment, but just take my word for it. Okay, so I've got this minimist thing now and rather than saying name equals process.argv. I can now say name equals args.name, now how is that gonna work.

[00:05:31] Let's come back to our command line make sure that you have the minimist installed so inside of my directory. I don't have node modules installed yet, so I could do npm install minimist. That's if you don't have node modules yet or if you don't have it installed. You just say npm install minimist, and when you hit Enter it should install it.

[00:05:58] To save bandwidth here at this place where we're hosting the event, I've already got it. So I'm just going to copy my entire node modules directory from the solutions folder that I've already installed. Just copy over all of those to my current directory, so if I go into that node modules directory and I look at what's there.

[00:06:20] You'll notice that there are several modules that I've already installed one of them being the minimist one that we care about. So we'll leave the rest of those alone we'll come back to the rest of those later. But right now I should now be able to say node 1.js.

[00:06:37] What happens if I provide no parameter whatsoever? We get our Hello undefined but now what happens if I do that node 1.js, but I gave it that name equals world syntax on the parameter command line? Remember before, it didn't do any parsing of it. Now when we do it it's able to have parsed out the command line for us.

[00:07:06]
>> Kyle Simpson: And there's a whole bunch, you can read the documentation. There's a whole bunch of other syntaxes that it supports and there's some configuration options or whatever. But it's a real simple first blush without us doing hardly anything. We now have the basic things in place for us to handle, writing command line programs in node, and that's the first kind of mind expander that I wanna give you.

[00:07:28] Node is not just about writing web servers. In fact, node is perhaps really pretty powerful in terms of replacing like. Bash scripting or Perl or things like that. I've just recently I kind of observed that I used to open up a bash script file whenever I wanted to do something very common like deleting files or renaming or things like that.

[00:07:51] I'd throw open up a bash script and I'd throw a find with an arc in a set in there and I'd give myself a little bash script file. I now find myself equally as often opening up a JavaScript file and making a node program out of it. In fact I'll show you something interesting.

[00:08:07] This is another thing that most that not a lot of people have we won't leave this in but for right now, I'll show you that you can do. The typical bash style annotation by saying let me make sure I get this exactly right cuz sometimes I forget the syntax so let me cheat real quick.

[00:08:25] Make sure I don't get it wrong.
>> Kyle Simpson: [COUGH]
>> Kyle Simpson: If you put this on the first line, usr/bin/env and then say node, you've turned this file into an executable script. In the same way we do a shell bash scripts where we would put bash but we're saying we want node to run our code.

[00:08:52] So now I can go back to my command line and instead of saying node 1.js, I can change the execution bits that I make by 1.js an executable script file. I can say 700 1.js and now I can say 1.js itself --name=World. And invoked node for me, so I can create command line programs that look and feel a lot like dash script or another pearl scripts and things like that.

[00:09:27] And I don't even have to have the .js extension on that. I could name the file just one or delete all or whatever kind of commands you want. So node is capable of providing that for you and as you get more familiar with working with JavaScript on the server.

[00:09:45] You may find yourself wanting to just, I'm already writing JavaScript I'll just write this script also in JavaScript. So I just wanted to give you a couple of quick ways that you can use JavaScript for it. We'll go ahead and take that out for now because that's not the plan to use it that way for the rest of our exercises but I just wanted to give you a sense that was pretty easy to do.