Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Every time we've run a Node program, we've done it from the command line. It will be very convenient to be able to configure our program, as we run it by specifying some command line arguments. Node provides tools we can use to do that. In this video, we'll write some code accepting command line arguments. First let's go to the Exercise Files and set up the project. Go to chapter 8 and video 3, and then Copy the Start folder onto the Desktop. Next go to Terminal, and change directory to that folder.
I'm just going to drag it in to get the path. Then press Return, and then type npm space install. And also follow that by Return. So once that's complete, open up the Start folder in your Text Editor, and first let's go to server.js. Now Node is already keeping track of what's being entered on the command line, and it's actually pretty easy to get to. Let's see what Node keeps track of. Type in console log and then process.argv. So now, let's go to terminal and start up the server.
I'm going to type in node space server, and now I'm going to pass in some arguments that I'd like to use in a moment. But for now we're just going to see how they come through in Node. So first I'm going to pass in flight as a flag. So dash, dash flight, and the number's going to be 567. And then another flag for destination. And I'm going to provide LAX as the value for that. So now press Return, and you'll see here that it comes back as an array. The first item is Node itself, and then the second item is the path to the file that's being run.
And then the remaining items are the flags and values that we passed. So this is great. We have access to all this data, but there's a bit of a problem. You notice we're getting an array here of arguments. This is nice, but it's still going to be a pain to pick through. It'd be much more helpful, if we could get this data back in an object, where we can look up the values by Property. It turns out the Node community provides several modules for doing just that. I'm going to install one of them named optimist.
So first I'm going to stop this server. I'm going to press Ctrl+C, and now I'm going to install optimist. Type npm install, and them dash, dash save. And then optimist. So now optimist has been saved and installed. Let's add it to our application. Go back to server.js, and now in the list of variables that get included, we're going to require optimist. We're going to set a new variable called argv, and we're going to require optimist.
And we're actually going to read the argv property from optimist. And then we're going to finish that with a comma. So now let's inspect that argv variable. Type in console log, and argv. So now we're going to compare what optimist gives us, with what we get from the process from node. To Save the file, and let's go back to the command line. I'm going to press up two times. And that's going to give me, node server --flight 567 and --destination LAX. And now when I press Return, I now get two items.
The first item, is the object that we get from optimist, and the second item is the array that we're getting from the process. So notice here, that the different flags are coming through as properties. So we have a property for flight, and the value is 567. And we have a property for destination, and the value is LAX. We also have another property called dollar sign zero, and that's showing us the command that was used to run this script. So this is exactly what I want. I now have an object, that has all the properties and values that are getting supplied on the command line.
So now let's use those values in our code. I'm going to stop this server by pressing Ctrl+C, and and now I'm going to go back to server.js. So now I'm going to get rid of both lines 11 and 12, because I don't need them anymore. And now I'm going to add some logic for handling argv. So what I want to do is make sure that flight and destination are both set, before doing anything with those values. If destination is set, that doesn't really tell me anything, because I need to know which flight to set the destination on.
So, let's do an if statement, and test to make sure that argv flight is set. And let's also make sure that argv destination is set. So if both of those properties are set, I'm going to set this data in the flight. So first we're going to look up that flight, by flight number. And then we're going to set the destination property of the data in that flight. So I'm going to press dot and then data, and then destination, and then I'm going to set that property to the value of argv destination.
So now I'm saving the file. First, let's go to the server and let's just start the server with no command line arguments. I'm just going to go node space server. And press Return. And now I'm going to go to the browser, and load up localhost at flight 567. So, at the moment, the destination is Denver. Now, let's try restarting the server with some command line arguments. I'm going to press Ctrl+C to stop the server. And then I'm going to press up two times. And that's going to get me the command line arguments, that I typed in earlier.
So, now I'm going to press Return. And, now it's saying that data is not defined. There's a slight problem with the logic in my code, and I'm going to fix that. So, let's go back to the Text Editor. Turns out, the name of the variable that's holding the data is actually flights. So, I'm going to change data to flights. Save the file. And now I'm going to press up to get that command again, and press Return, and now it says, the Express server is listeing on port 3000. So I can go back to web browser and hit Refresh, and now the destination is set to LAX.
Modifying an application to receive command line arguments, is made easier through the use of modules like Optimist. Optimist provides a convenient object containing everything from the command line. In the next video, we'll build a complete Command Line tool.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 105366 Viewers
56 Video lessons · 117088 Viewers
71 Video lessons · 86306 Viewers
131 Video lessons · 41247 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.