WEBVTT 00:06.170 --> 00:10.010 In this video you're going to build your very first node up. 00:10.010 --> 00:19.430 You will also get introduced to some similarities and differences between node or JavaScript in no jazz 00:19.700 --> 00:22.460 and JavaScript in the browser. 00:22.490 --> 00:33.270 So first I'm going to create a folder inside the node folder and call it your first node up. 00:33.340 --> 00:42.920 So just to make things tidy I'm going to open only this folder for now inside rockets so I'm going to 00:43.040 --> 00:50.450 show it's in the finder and just drag it in brackets. 00:50.470 --> 00:57.250 So now let's create a file and call it up those days. 00:57.310 --> 01:05.320 So this is the file where we're going to write our javascript code and then after that we'll be able 01:05.320 --> 01:07.950 to run our node up. 01:08.020 --> 01:10.620 So let's start with something really simple. 01:10.890 --> 01:11.640 A console. 01:11.650 --> 01:14.350 The log statements 01:17.030 --> 01:22.860 and see how we can run this simple up. 01:22.910 --> 01:30.860 So I suggest going for something similar to this in terms of layout just to make things clear and easy. 01:31.670 --> 01:38.100 So at the moment I am inside this. 01:38.100 --> 01:41.240 For that so I need to move inside this one. 01:41.900 --> 01:47.870 So let's go to the desktop and then node and then 01:51.270 --> 01:55.830 say by the way you see the backslash is there just to escape the spaces. 01:55.920 --> 01:56.900 All right. 01:57.600 --> 02:00.500 So now we are in the correct folder. 02:00.600 --> 02:09.690 So you are now ready to run the app so to run the app the command is node and then followed by the name 02:09.720 --> 02:11.020 of the app. 02:11.070 --> 02:13.020 So let's try this. 02:13.020 --> 02:14.040 And here we go. 02:14.040 --> 02:17.240 You can see the outputs of the console. 02:17.250 --> 02:18.200 Not log. 02:18.230 --> 02:24.830 Oh this is very similar to what you get in the browser developer console. 02:24.900 --> 02:38.130 So if you go to Chrome for example and go to the developer tools open the console then if you type console 02:38.130 --> 02:38.670 the log 02:43.530 --> 02:47.860 high you'll get the outputs in the console. 02:48.690 --> 02:53.480 So in node you get the outputs in the terminal. 02:53.610 --> 02:58.130 Now you can decide to place the outputs inside a file. 02:58.230 --> 03:06.130 So to do that you can just use the double then great on sign and you the. 03:06.200 --> 03:17.360 Finally you can create a file both text for example and this way you can append whatever you're on in 03:17.360 --> 03:20.050 this command inside that mine. 03:20.450 --> 03:23.510 So you're not gonna do that for now. 03:23.510 --> 03:33.670 All right so now let's add some more content inside our file so let's art another console log statement. 03:35.460 --> 03:36.290 Then another one 03:47.810 --> 03:49.630 OK. 03:49.740 --> 03:51.810 And if you try this again 03:59.710 --> 04:02.940 you get the full outputs. 04:02.950 --> 04:10.050 All right so now let's get introduced to some important variables in note. 04:10.090 --> 04:13.610 So the first one is the process variable. 04:13.810 --> 04:23.010 So the process variable is the equivalent of the documents variable inside the browser. 04:23.770 --> 04:29.230 So if you go to the browser and you console log 04:32.910 --> 04:33.480 documents 04:36.560 --> 04:37.640 this is what you get 04:40.570 --> 04:41.850 inside documents. 04:41.920 --> 04:45.980 You can see all the contents of your web pages. 04:46.030 --> 04:47.100 All right. 04:47.200 --> 04:52.660 And documents comes with some really handy methods. 04:52.660 --> 04:56.800 So for example you can get elements by I.D. 04:59.840 --> 05:02.470 and you can do so many other things. 05:02.640 --> 05:09.330 Now in node the equivalent of documents is process 05:15.590 --> 05:20.540 so let's try to log process and see where we get 05:26.070 --> 05:29.250 so this is what's the process variable. 05:29.370 --> 05:29.940 Looks like 05:34.290 --> 05:38.500 you can see so many properties inside this object. 05:38.550 --> 05:48.970 So if you want to know more about this global objects you can head to the node website and go through 05:48.970 --> 05:50.160 the documentation. 05:50.380 --> 05:59.770 So docs and then are we looking at the latest version at the moment and then you can go to process. 06:00.940 --> 06:06.180 So inside process you'll find so many things. 06:06.180 --> 06:11.890 So for example let's check process dot org V. 06:12.910 --> 06:22.210 So this one will return an array containing the command line arguments that you passed when you launched 06:22.210 --> 06:24.650 your node process. 06:24.970 --> 06:26.740 So for example if you're on this 06:30.230 --> 06:38.750 followed by arguments then the outputs would be the location of the node executable. 06:38.750 --> 06:44.300 This comes first followed by the different arguments of your command. 06:44.300 --> 06:48.360 So the first argument is the file so you know a case 06:55.150 --> 06:58.200 it was up those two years and with the impasse. 06:58.210 --> 06:59.750 Any other arguments. 06:59.770 --> 07:08.250 So if we look for ARG V in our case you can see that we've got only two elements the location of our 07:08.250 --> 07:15.180 executable and then the up DOJ s file and it is the full path of our file. 07:15.230 --> 07:20.720 Now if if we try this with extra arguments. 07:20.720 --> 07:27.410 So for example I'm going to add First Name Last Name. 07:28.010 --> 07:35.680 Then if we check on V this time we'll find a couple of more elements. 07:35.680 --> 07:35.950 All right 07:40.180 --> 07:43.280 so it is regarding V. 07:43.300 --> 07:52.180 So now let's check another one process dots and this is another important one which we'll be using later. 07:52.220 --> 07:57.340 So inside this one you will see information about the environments of the user. 07:57.340 --> 08:04.540 So for example you can see the shell and this is the four parts of your bash executable you remember 08:04.540 --> 08:13.000 that from the previous video you can see the path variable cover this one as well. 08:13.260 --> 08:17.640 And you can see also the home directory of the current user. 08:17.730 --> 08:18.660 All right. 08:18.770 --> 08:26.440 So this is a quick overview of the process variable and it's a very important global object. 08:26.450 --> 08:36.110 Now let's go back to the terminal and check another important variable which is 08:45.180 --> 08:45.630 global 08:52.210 --> 08:57.880 so global is very similar to the window global objects in the browser. 08:58.840 --> 09:09.100 So this one will include functions like play interval play time our set interval so functions that come 09:09.100 --> 09:14.110 with no Jesus just like they are attached as an add on to the browser. 09:14.110 --> 09:23.260 So by the way functions like set time out they are actually parts of the dorm API is they don't they 09:23.260 --> 09:31.210 don't come with JavaScript satirically and similarly in node a function like this one is actually provided 09:31.210 --> 09:38.140 by Newt not by the v 8 JavaScript engine. 09:38.160 --> 09:38.460 All right. 09:38.670 --> 09:44.440 So you won't find this one in the scripts specification for example. 09:44.970 --> 09:48.570 So if you want to check that you can go to 09:51.580 --> 09:52.780 the node Web sites 09:57.660 --> 10:01.800 and in the documentation look for time is 10:04.720 --> 10:06.690 then let's look for 10:09.810 --> 10:12.910 said time out. 10:13.140 --> 10:14.060 All right. 10:14.230 --> 10:19.940 And you can see that say time out was added in. 10:20.190 --> 10:24.860 Note inversion zero point zero point one. 10:24.940 --> 10:25.500 All right. 10:25.630 --> 10:29.970 So this is not part of the V8 engine or Javascript based engine. 10:30.100 --> 10:36.970 It's actually part of node itself just like it is attached to the browser API. 10:37.000 --> 10:44.550 Now if you want you can compare say time out to a function like month dot random so you'll see that 10:45.370 --> 10:46.990 set time outs is actually 10:50.990 --> 10:56.520 a tied to this object. 10:56.610 --> 10:58.260 Well math the random 11:02.900 --> 11:07.820 is actually part of it was scripts. 11:08.340 --> 11:13.110 So let's save our file and run the op again. 11:14.070 --> 11:23.360 Well let's comment this line and try again. 11:23.780 --> 11:28.320 So that's our global object. 11:28.320 --> 11:28.770 All right. 11:29.480 --> 11:36.390 And you can see inside these objects functions like clear interval clear time out say time out and so 11:36.390 --> 11:40.580 on Perfect. 11:40.640 --> 11:48.200 All right now let's create another simple app which will just greet a user if they provide their first 11:48.200 --> 11:50.060 name and last name. 11:50.060 --> 11:59.910 So in this scenario we're going to pass to the node command both the name of our new file then the arguments 11:59.970 --> 12:02.230 first name and then last name. 12:02.280 --> 12:08.050 So let's create a new file and call it greed those days. 12:08.580 --> 12:15.950 So first we need to retrieve the arguments from the command. 12:17.130 --> 12:21.060 So we can define the first name variable 12:26.870 --> 12:31.970 so we're gonna go to the process variable and then go to ARG V. 12:32.240 --> 12:38.490 So if you remember the first element was the full pass of the node executable. 12:38.660 --> 12:46.460 Then the second one was the file you pass to the node command and then comes the next arguments. 12:46.460 --> 12:49.650 So we're looking aren't position two. 12:50.240 --> 12:52.220 All right. 12:52.220 --> 12:55.460 And then we need the last name. 12:57.180 --> 13:01.160 And that's gonna be in position three. 13:01.500 --> 13:03.830 It's then that the next one. 13:03.900 --> 13:04.780 All right. 13:04.780 --> 13:05.710 So now 13:09.730 --> 13:11.710 what we wants we want to log 13:14.270 --> 13:16.070 hello first name then last name 13:24.420 --> 13:34.600 so you just need to make sure that you are comfortable with the E6 features otherwise you can just go 13:34.600 --> 13:39.650 back to the E6 bonus section. 13:39.730 --> 13:41.440 So now this should work. 13:41.470 --> 13:43.430 So let's try this. 13:43.480 --> 13:51.300 So I'm gonna run node greets Borges and pass something like 13:54.040 --> 13:57.940 Steve Jobs and you can see here. 13:57.970 --> 13:58.910 Hello Steve Jobs. 13:58.970 --> 14:03.150 So it's just odd an exclamation mark right there. 14:06.370 --> 14:08.170 This looks better. 14:13.150 --> 14:13.610 OK. 14:13.620 --> 14:14.790 So now let's 14:19.890 --> 14:29.280 improve our code so that if we pass the first name and last name like this we print the first name and 14:29.280 --> 14:32.990 last name starting with a capital letter. 14:33.180 --> 14:41.400 So there is a function in javascript which is to uppercase which is a method of the string objects which 14:41.400 --> 14:47.120 will turn the whole first name or last name to capital letters. 14:47.120 --> 14:53.690 But we want to capitalize only the first letter so to do this what you can do. 14:53.910 --> 14:59.190 So first of all we're gonna be changing the first name so it's going to be letters instead of constant 15:01.430 --> 15:02.270 All right. 15:02.360 --> 15:03.500 So first of all 15:06.410 --> 15:10.090 we need to capitalize the first letter. 15:10.370 --> 15:12.740 So we gonna get the first letter. 15:12.740 --> 15:17.590 So we're going to store everything inside first name so we get the first letter of first name. 15:17.750 --> 15:25.640 So that's the elements R's position zero insight first name and then we turn it to uppercase 15:30.900 --> 15:32.020 like that. 15:32.610 --> 15:39.240 And then after that we need to print the remaining of first name. 15:39.240 --> 15:44.340 So we start out position one and we keep going. 15:44.340 --> 15:45.990 And there is a useful function for that. 15:46.020 --> 15:50.170 It's substring. 15:50.470 --> 15:54.230 So if you pass to substring only one argument. 15:54.250 --> 16:02.540 So for example one then he's going to return the string starting from position 1. 16:02.590 --> 16:12.530 So for example if we've got Steve then the first letter is going to be first name position zero which 16:12.530 --> 16:19.850 we turn to uppercase and then first name dot substring of 1 will start us position 1 and keep going 16:19.850 --> 16:20.590 until the end. 16:20.630 --> 16:23.380 And he's going to return this. 16:23.430 --> 16:31.030 So at the end you will get capital S plus T. 16:31.160 --> 16:32.000 All right. 16:32.000 --> 16:33.560 And let's do the same thing 16:38.110 --> 16:41.210 or the last name or just creates a function if you like 16:44.000 --> 16:45.480 and let's call it 16:49.810 --> 16:53.290 a first. 16:53.520 --> 16:53.890 All right. 16:54.040 --> 16:56.050 So this function will take 17:01.410 --> 17:05.700 a string and will return this 17:08.860 --> 17:15.130 OK let's replace this with up it first of those name 17:21.470 --> 17:26.250 and then this with uppercase of last name. 17:26.490 --> 17:33.990 So let's try our code and it's working perfectly. 17:34.030 --> 17:34.390 All right. 17:34.390 --> 17:42.010 So in the next video you're going to move to file exports which will allow you to pass variables and 17:42.010 --> 17:45.910 functions or methods between different JavaScript files.