Coding

Getting Started with Node.js – Full Tutorial

  • 00:00:00 hello thanks for tuning in this is a
  • 00:00:02 recording of an online training session
  • 00:00:04 which was a beginner level class on the
  • 00:00:05 node.js runtime this was a freestyle
  • 00:00:08 interactive no slides just me typing
  • 00:00:11 good and talking about it so let me
  • 00:00:12 apologize in advance for all the speech
  • 00:00:15 fillers I did however cut out the pauses
  • 00:00:18 and the not useful parts so you can
  • 00:00:20 watch the whole training here on YouTube
  • 00:00:22 in one video it's 2.5 hours but it's
  • 00:00:25 also available at Gia's complete dot-com
  • 00:00:27 /gs and 19 where i split it by topic
  • 00:00:30 into many videos you can also use the
  • 00:00:32 timestamps links in the description to
  • 00:00:33 navigate to the different sections if
  • 00:00:35 you think of any questions while you
  • 00:00:37 watch this please feel free to join the
  • 00:00:39 JISC complete slack channel I just
  • 00:00:41 complete that comm slash help and ask us
  • 00:00:43 there I hope you enjoyed this class and
  • 00:00:45 please share it with your network if you
  • 00:00:47 know anyone who plans on exploring
  • 00:00:49 nodejs let's get started so this is a
  • 00:00:53 beginner class on node so uh no
  • 00:00:55 assumptions about what you know about no
  • 00:00:57 this is your first steps you know I
  • 00:01:00 might be assuming that you know a little
  • 00:01:03 bit of JavaScript and I am definitely
  • 00:01:05 assuming that you know programming in
  • 00:01:07 general so depending on on your skills
  • 00:01:09 you might be you might be overwhelmed or
  • 00:01:12 you might be disappointed with the
  • 00:01:13 content we all have different skills and
  • 00:01:15 this is natural but I hope most of you
  • 00:01:19 will learn a few things anyway the class
  • 00:01:21 is recorded so if you're lost in the
  • 00:01:24 class go fill the gap in your knowledge
  • 00:01:26 and come back and watch the recorded
  • 00:01:28 class so my name is Sam Urbana and I
  • 00:01:32 have been doing an ode for a while
  • 00:01:34 currently I do note on the back end and
  • 00:01:38 I do react on the front end but before
  • 00:01:41 that I did a lot of other programming
  • 00:01:43 languages I did some tickle Ruby I did a
  • 00:01:46 lot of pee sequel but now it's all node
  • 00:01:49 so and I am a really big fan of node and
  • 00:01:53 everything all the libraries that make
  • 00:01:56 that are added on top of the JavaScript
  • 00:01:59 language which itself is very flexible
  • 00:02:01 and it can do whatever you want it to do
  • 00:02:04 so if you want to see the stuff that I
  • 00:02:06 do I put links here feature links on on
  • 00:02:09 this website and also I publish
  • 00:02:11 everything I do on juice
  • 00:02:12 complete calm so there are video courses
  • 00:02:15 books interactive labs and a lot more
  • 00:02:18 Jes complete calm by the way is a node
  • 00:02:20 project so this is written in node and
  • 00:02:23 react on the front end so I have been
  • 00:02:27 hacking on this platform and it's really
  • 00:02:30 fun so no no this node is really funny
  • 00:02:33 well at least for me so let me set some
  • 00:02:37 expectations about the class today
  • 00:02:40 before we begin so this is not going to
  • 00:02:44 make you an expert on node right this is
  • 00:02:47 just your first step so just to get you
  • 00:02:50 familiar to raise your confidence in the
  • 00:02:52 platform and to get you started on the
  • 00:02:56 right path to learning node because I
  • 00:02:58 think a lot of people learn node the
  • 00:03:00 wrong way they focus on the packages
  • 00:03:03 that that comes with node or like that
  • 00:03:05 the third-party packages that are very
  • 00:03:07 useful but I think there is a big
  • 00:03:11 benefit in to learning the internal you
  • 00:03:14 know the the raw runtime before you go
  • 00:03:17 and use the packages and and that
  • 00:03:19 knowledge is gonna make you make your
  • 00:03:22 use of those packages much better so uh
  • 00:03:25 the one thing that you need for sure is
  • 00:03:27 you need node so so if you have a node
  • 00:03:32 version let me make this a little bit
  • 00:03:35 bigger if you have a node version that
  • 00:03:38 is 10 or higher you're fine I think it
  • 00:03:42 will be fine too but if you have an
  • 00:03:43 older node version so go ahead and
  • 00:03:45 install node I encourage you to always
  • 00:03:49 experiment with the latest and greatest
  • 00:03:52 always have the at least the latest LTS
  • 00:03:55 release but I actually do my development
  • 00:03:58 on the current version even if it's an
  • 00:04:01 odd number because odd number they're
  • 00:04:02 not gonna be LTS so the way this works
  • 00:04:05 is even numbers are LTS which is long
  • 00:04:08 term support odd numbers are not so
  • 00:04:11 right now we don't have any odd numbers
  • 00:04:13 this is an even number but it's not
  • 00:04:14 LTS yet it goes into LTS maybe in a few
  • 00:04:19 months right so LTS will become no 12
  • 00:04:21 will become LTS and they start with node
  • 00:04:23 13 which is not LTS it's
  • 00:04:25 experimental but always try to have the
  • 00:04:28 latest node so that you get to start
  • 00:04:31 exploring the new features that come to
  • 00:04:33 language and so that you detect problems
  • 00:04:35 going away because once you have the
  • 00:04:37 latest if you using something that is
  • 00:04:39 deprecated you're gonna get warnings and
  • 00:04:41 it's just so much better overall but
  • 00:04:44 definitely you have to test your systems
  • 00:04:47 on the LTS that is that you're gonna use
  • 00:04:51 in production right so for example just
  • 00:04:55 complete comm is running right now on
  • 00:04:57 this LTS so even though I develop I'm
  • 00:05:00 and I add features on 12440 if I have it
  • 00:05:04 I still locally I still switch things to
  • 00:05:08 the LTS and test things on LTS just give
  • 00:05:12 the whole system I go on the LTS before
  • 00:05:14 releasing a big release so there are
  • 00:05:17 tools for you to switch around so if
  • 00:05:21 you're on Mac you should definitely have
  • 00:05:23 homebrew homebrew is just the best way
  • 00:05:25 to to this one the missing package
  • 00:05:29 manager from node the best way to
  • 00:05:32 install node and install many other
  • 00:05:33 packages around node but there's also
  • 00:05:38 nvm so nvm and there's a version for it
  • 00:05:41 for Windows as well is note version
  • 00:05:43 manager so if you have nvm install now
  • 00:05:46 you can install node and you can install
  • 00:05:48 different versions of node and you can
  • 00:05:50 switch between these versions with the
  • 00:05:54 press of a command so you get to
  • 00:05:56 experiment with many versions of node
  • 00:05:59 and that's that's great so what is what
  • 00:06:03 is node exactly so when you have the
  • 00:06:05 node command when you install node you
  • 00:06:07 get three commands you get the node
  • 00:06:09 command you get the NPM command and you
  • 00:06:13 get the npx command all of these comes
  • 00:06:15 built in with node
  • 00:06:17 so node is a wrapper around v8 so v8 if
  • 00:06:24 you're not familiar with v8 v8 is the
  • 00:06:27 JavaScript engine not the juice the
  • 00:06:29 JavaScript engine man the a JavaScript
  • 00:06:34 the JavaScript engine that powers chrome
  • 00:06:38 itself
  • 00:06:39 so I am in Chrome right now and if I go
  • 00:06:43 in here and open up the console and put
  • 00:06:46 in any JavaScript so map the random for
  • 00:06:49 example this is a JavaScript function
  • 00:06:51 this is the JavaScript standard library
  • 00:06:53 and chrome just executed that JavaScript
  • 00:06:56 it did that through v8 so they extracted
  • 00:07:00 everything JavaScript from chrome into
  • 00:07:02 this v8 engine and of course every
  • 00:07:04 browser has its ancient a different
  • 00:07:06 engine so IE has a chakra there's chakra
  • 00:07:10 there's spider monkey and and a few of
  • 00:07:14 them now this is different then
  • 00:07:18 executing a browser API so for example
  • 00:07:21 if I do document dot get element by ID
  • 00:07:25 right and or get element by whatever
  • 00:07:28 that is that is Dom API that has nothing
  • 00:07:34 to do with JavaScript it just have a
  • 00:07:36 JavaScript interface so the this is a
  • 00:07:40 JavaScript object document is a
  • 00:07:42 JavaScript object but get element by ID
  • 00:07:44 is an API on that object
  • 00:07:46 same thing for things like set timer set
  • 00:07:49 timeout set interval these are just
  • 00:07:51 functions that gives you access to the
  • 00:07:53 browser API these are not JavaScript so
  • 00:07:56 set time app is not implemented by v8
  • 00:07:59 it's implemented by the browser itself
  • 00:08:01 so when you get the browser you have two
  • 00:08:03 things you have v8 that runs everything
  • 00:08:04 JavaScript and you have the browser
  • 00:08:06 itself that runs everything DOM and
  • 00:08:10 scrolling for example a handle scrolling
  • 00:08:13 and handle resizing all of these events
  • 00:08:15 are coming from the browser but here's
  • 00:08:17 the thing everything runs in a single
  • 00:08:19 thread so if you have a big giant for
  • 00:08:23 loop in here that takes for a billion
  • 00:08:25 times that loop is gonna block the
  • 00:08:27 browser from doing things like scrolling
  • 00:08:29 so if the for loop is running the
  • 00:08:31 background the user cannot scroll your
  • 00:08:33 page so it is important for you to
  • 00:08:36 understand that this is a single
  • 00:08:37 threaded environment however the browser
  • 00:08:40 gives you an API a synchronous API is to
  • 00:08:44 do things asynchronously so that you
  • 00:08:46 don't have to block as a single thread
  • 00:08:49 that you have the exact same story is
  • 00:08:53 note so note just like the browser is
  • 00:08:56 also a wrapper around v8 so you get to
  • 00:08:59 execute so if we have the note command
  • 00:09:02 just type in the note command node and
  • 00:09:05 you get into this node that we call
  • 00:09:07 grapple mode run eval print loop and
  • 00:09:11 it's the exact same mode that you get
  • 00:09:13 when you open the browser's console this
  • 00:09:15 is also a rebel mode so in here you can
  • 00:09:19 just do math dot random math dot random
  • 00:09:21 and you get the exact same thing so this
  • 00:09:24 is through node right but note we really
  • 00:09:27 didn't do anything here know just pass
  • 00:09:29 this command to v8 and v8 executed math
  • 00:09:32 dot random v8 old node what the result
  • 00:09:35 of math.random is and note printed it to
  • 00:09:38 our screen right here so it's going back
  • 00:09:41 and forth between node and mv8
  • 00:09:44 so the question is why can't we just use
  • 00:09:46 the eight directly you can of course but
  • 00:09:49 no it comes with a lot of other benefits
  • 00:09:52 around the the core standard library
  • 00:09:54 right for example there is let's say
  • 00:09:58 nationally to be server you can just
  • 00:10:00 start using an HTTP servers there was a
  • 00:10:02 package here called HTTP and it gives
  • 00:10:05 you access to starting and HTTP server
  • 00:10:07 and just like HTTP there is a lot of
  • 00:10:10 them in fact let's take a look at all of
  • 00:10:13 them right away so I'd like to point
  • 00:10:16 your attention to a very very important
  • 00:10:19 key on your keyboard ready it is the tab
  • 00:10:22 key which is above the caps lock right
  • 00:10:27 this tab key is important because of the
  • 00:10:30 discoverability it gives you discover
  • 00:10:32 discoverability powers so for example if
  • 00:10:35 you are if array is a javascript class
  • 00:10:39 right and it has many methods if you tap
  • 00:10:42 DoubleTap on array dot so just do array
  • 00:10:45 dot and double tap you get everything
  • 00:10:48 that the available on array this is this
  • 00:10:51 is a great discoverability because I
  • 00:10:53 don't have to look up the things that
  • 00:10:55 you can do especially if you have an
  • 00:10:56 object like a mystery object that you
  • 00:10:58 don't know what it is right so if you
  • 00:11:00 have give you em and you don't know what
  • 00:11:02 M is you can just see all the methods
  • 00:11:04 that you can do
  • 00:11:06 that look like em is an array because I
  • 00:11:07 get to join and split than Keys and
  • 00:11:10 splice and reverse so it isn't the radio
  • 00:11:12 everything I can do and if you double
  • 00:11:14 tap on an empty screen on an empty line
  • 00:11:19 you get everything that you can start
  • 00:11:22 with so all the JavaScript classes
  • 00:11:25 you'll see here all the JavaScript
  • 00:11:26 classes because you know it's a
  • 00:11:28 wraparound JavaScript but also get
  • 00:11:30 everything that is note so you see HTTP
  • 00:11:33 HTTPS everything that starts with a
  • 00:11:36 lowercase letter most of the things that
  • 00:11:38 start with a lowercase letter here are
  • 00:11:40 built in node so libraries that just
  • 00:11:44 come with node you don't have to install
  • 00:11:45 them those are things that you can just
  • 00:11:47 start using so node is not a product
  • 00:11:52 from chrome node is an open-source
  • 00:11:55 community managed project but it uses a
  • 00:12:00 product from Google which is v8 so v8 is
  • 00:12:02 Google Google manages v8 and a node uses
  • 00:12:06 v8 and it uses other stuff not just v8
  • 00:12:08 for example it uses open SSL and a few
  • 00:12:13 other libraries like that also you can
  • 00:12:16 use node with other VMs so you don't
  • 00:12:18 have to so there's an experiment or a
  • 00:12:20 project where you can use node with
  • 00:12:22 chakra which is the chakra core the ie
  • 00:12:25 thing you know the powers IP edge so you
  • 00:12:30 can run node without v8 with using
  • 00:12:33 chakra core but by default when you
  • 00:12:36 install node you get the node that's
  • 00:12:38 wrapped around v8 ok cool so this list
  • 00:12:43 here is really important in fact I would
  • 00:12:46 say if you want a master node you have
  • 00:12:48 to learn every single item on this list
  • 00:12:50 I still I'm still learning new stuff
  • 00:12:53 because new stuff comes all the time but
  • 00:12:57 most of this stuff is JavaScript so if
  • 00:13:00 you are not familiar with things like
  • 00:13:02 date function not a number errors object
  • 00:13:06 reflect simple these are JavaScript
  • 00:13:09 things week map modern JavaScript right
  • 00:13:12 it buffers array integers array floats
  • 00:13:15 are a map promise regular Express
  • 00:13:19 Shen's weeks that all of these are
  • 00:13:21 modern JavaScript that you get to use
  • 00:13:23 out of the box with note and the other
  • 00:13:27 stuff that I'd like to point out to the
  • 00:13:29 things that we will explore today we so
  • 00:13:34 console is is a the same library that
  • 00:13:38 the same you know object that you have
  • 00:13:40 in your in your browser's console and
  • 00:13:41 they try to mirror the exact same
  • 00:13:45 interface for it in node but console is
  • 00:13:48 not-it's not v8 it's written in node and
  • 00:13:50 it's in the browser it's not v8 it's the
  • 00:13:52 browser itself so today we're going to
  • 00:13:55 talk about console we're going to talk
  • 00:13:56 about the module object will mention
  • 00:13:59 path will mention timers we're going to
  • 00:14:01 talk about timers in general we're also
  • 00:14:04 gonna talk about HTTP maybe a little bit
  • 00:14:07 of net so there's a question what is M
  • 00:14:10 p.m. how is it related to u node we're
  • 00:14:12 gonna talk about that so you're running
  • 00:14:14 node.js on shell on Mac what I should
  • 00:14:17 run on Windows command prompt yeah so on
  • 00:14:20 Windows you can run the CMD I would
  • 00:14:23 recommend that you instead of CMD that
  • 00:14:27 you install PowerShell so power fell by
  • 00:14:30 in Microsoft is just a much better
  • 00:14:33 terminal than CMD and by the way there
  • 00:14:36 is a new terminal coming to Windows so
  • 00:14:38 that is gonna be much better right but
  • 00:14:41 PowerShell is the way to give for now
  • 00:14:44 what is the IDE tool to cold the IDE
  • 00:14:48 that I'm gonna use is called vs code so
  • 00:14:52 it is probably the most popular editor
  • 00:14:59 so if you wanna run anything I'm working
  • 00:15:02 on we're gonna use it today and I would
  • 00:15:04 recommend that you use it now in fact it
  • 00:15:06 on Windows I vs code will give you a
  • 00:15:09 better terminal so you can use the
  • 00:15:11 terminal in vs good instead of using you
  • 00:15:14 know the command prompt or anything all
  • 00:15:17 right
  • 00:15:18 what is I term to so I turn to is just a
  • 00:15:20 replacement for the built in terminal
  • 00:15:23 it's just a little bit better than the
  • 00:15:26 built in terminal has better team ox
  • 00:15:28 integration but you don't have to use it
  • 00:15:30 but get put
  • 00:15:31 only less explore it if you if you see
  • 00:15:34 node is not a recognized command that
  • 00:15:37 means you have to fix the path so you
  • 00:15:40 have to put the node that was installed
  • 00:15:43 in the path so we talked a little bit
  • 00:15:46 about node rebel right node also is a
  • 00:15:49 command-line tool so if you do node – H
  • 00:15:53 you get everything that you can do in
  • 00:15:56 node as a command-line tool we see a lie
  • 00:15:59 of node so this is this is the CLI
  • 00:16:01 that's gonna run a script in general but
  • 00:16:04 it also you can use it with other
  • 00:16:07 options so you'll see the usage here
  • 00:16:09 node options and then you give it a
  • 00:16:11 script if you want it to run a script
  • 00:16:13 another another usage is node inspector
  • 00:16:16 we'll talk about that how to inspect a
  • 00:16:18 script in node and all of these options
  • 00:16:20 just scan through them they're handy I'd
  • 00:16:23 like to point out one particularly or a
  • 00:16:29 few particularly helpful options if you
  • 00:16:31 want to check the syntax of an ood file
  • 00:16:33 and not run it just check the syntax –
  • 00:16:36 see if you want to execute don't use e
  • 00:16:39 use P because P is execute and print so
  • 00:16:44 I'll sure let's let's explore that so if
  • 00:16:47 I have no – P and you just give it any
  • 00:16:51 JavaScript and it will execute it and
  • 00:16:53 print it right away so that p is for
  • 00:16:56 print a handy to just do things really
  • 00:17:00 quick and and you and the other one that
  • 00:17:03 i'd like to point your attention to is
  • 00:17:05 v8 options v8 options so if you do – –
  • 00:17:09 v8 options what you get is a big list of
  • 00:17:13 all the options that node can pass to v8
  • 00:17:18 so let's let's explore that I'm gonna
  • 00:17:19 pipe it on less so that we get at page
  • 00:17:21 by page so if he ate options so in here
  • 00:17:24 you're gonna see a lot of things right
  • 00:17:26 you're gonna see harmony flags you're
  • 00:17:30 gonna see tracing flags and this listed
  • 00:17:33 like 400 or 500 options that you can do
  • 00:17:36 and and these are options that note pass
  • 00:17:38 directly to v8 so if you wanna change
  • 00:17:43 the way v8 be
  • 00:17:44 change the or a little optimization of
  • 00:17:48 the way v8 behaves or or change some
  • 00:17:51 experimental flags right if you want to
  • 00:17:53 experiment with you know webassembly
  • 00:17:56 right here there's a flags from rep
  • 00:17:57 assembly so most of these options are
  • 00:17:59 actually advanced I just want you to
  • 00:18:01 know of them so you can come to these
  • 00:18:04 options and and and search if you if
  • 00:18:07 you're looking to do party some
  • 00:18:08 something particular with with the like
  • 00:18:11 something advanced like like for example
  • 00:18:13 if you're if you're to work with the
  • 00:18:15 garbage collector in the a drive v8 has
  • 00:18:17 a garbage collector you have some
  • 00:18:20 options here to control the garbage
  • 00:18:23 pointer to expose the garbage collector
  • 00:18:25 to manually run the garbage collector so
  • 00:18:27 you'll come here and see all the options
  • 00:18:30 that you can do alright cool
  • 00:18:32 another thing that you should be
  • 00:18:34 familiar with is node environment
  • 00:18:37 variables so when you run a node process
  • 00:18:42 right when you run the node command
  • 00:18:44 whether you put in a file or you just
  • 00:18:46 run it with you know the rebel mode well
  • 00:18:49 what I did here is I started an
  • 00:18:51 operating system process right a process
  • 00:18:55 with a process ID and node I exposed
  • 00:18:59 this process object so there's a process
  • 00:19:02 object that has a few methods a lot of
  • 00:19:04 methods actually and this process object
  • 00:19:07 is basically the interface between your
  • 00:19:10 node environment and the operating
  • 00:19:13 system environment so if you need to
  • 00:19:15 read anything from the operating system
  • 00:19:17 or if you need to write anything to the
  • 00:19:19 operating system then you use this
  • 00:19:22 process object so it's the interface so
  • 00:19:25 this interface for example exposes the
  • 00:19:28 environment variables so all the
  • 00:19:30 environment variables that you have in
  • 00:19:31 your system just with process the in
  • 00:19:33 this give you access to everything
  • 00:19:35 invite in the environment that you have
  • 00:19:38 which is a cool way to customize your
  • 00:19:41 notes session so you just put something
  • 00:19:43 in the environment hopefully I don't
  • 00:19:44 have any passwords in here you put
  • 00:19:46 something in the environment and you can
  • 00:19:48 just access it in node right including
  • 00:19:52 things like you know including
  • 00:19:58 the built-in environment variables so so
  • 00:20:01 so the built-in environment variables
  • 00:20:03 are special environment variables that
  • 00:20:07 if you set them if you set them then you
  • 00:20:10 you get to change the behavior of note
  • 00:20:13 however before I talk about that let me
  • 00:20:15 show you how to control the environment
  • 00:20:16 is this is this is important so let's
  • 00:20:19 say no – P and let's let's print process
  • 00:20:24 the end the user user is a standard
  • 00:20:30 environment variable right so this is
  • 00:20:31 the user that I'm using if you want to
  • 00:20:33 change that all you need to do is just
  • 00:20:35 specify a different user right and no it
  • 00:20:39 will just use a new value that you put
  • 00:20:41 in here instead of you know that's set
  • 00:20:43 in value because I changed the
  • 00:20:44 environment variable before executing
  • 00:20:47 node and of course I can do export or
  • 00:20:49 step on Windows to change it for to
  • 00:20:53 change it forever you cannot change
  • 00:20:55 there's a very good question from Amith
  • 00:20:58 you cannot change the environment
  • 00:20:59 variable through process the end it's
  • 00:21:01 not it's read-only things although you
  • 00:21:04 can actually change it so you can do you
  • 00:21:08 know equal tests but this will not
  • 00:21:11 change the environment variable here
  • 00:21:14 okay so I think if you do that right and
  • 00:21:18 try to echo a dollar sign user after
  • 00:21:21 that it did not change it so node will
  • 00:21:25 not tell you that oh you can't change it
  • 00:21:27 it's not really immutable but you get a
  • 00:21:33 copy you don't you get a copy of the
  • 00:21:36 object all right so let's keep going
  • 00:21:38 if you have so if you want to change
  • 00:21:40 some kind of behavior I'd like to point
  • 00:21:42 your attention to a few of these
  • 00:21:45 environment variables one one one handy
  • 00:21:49 in particular is note debug so if you do
  • 00:21:53 if you use a library like for example
  • 00:21:55 HTTP and you'd like node to debug more
  • 00:21:59 information about what it's doing in
  • 00:22:02 HTTP you can you can do something like
  • 00:22:05 this so you can note debug I think you
  • 00:22:07 just do HTTP and then run your node
  • 00:22:09 command
  • 00:22:10 and this will tell node to debug more
  • 00:22:12 information when it uses the HTTP
  • 00:22:14 library so very handy when you start
  • 00:22:17 running into problems you can change its
  • 00:22:19 a path so this is another important one
  • 00:22:22 that we're gonna talk about we're gonna
  • 00:22:23 actually talk about that next it is how
  • 00:22:26 to control how no discover modules so
  • 00:22:30 let's go ahead and jump into the more
  • 00:22:32 serious mode which is to run node in to
  • 00:22:36 run a script so I'm gonna create a
  • 00:22:38 directory so node I'll just do 0 6 19
  • 00:22:45 and I'm gonna go into this directory not
  • 00:22:48 this one and I'll just run vs code on
  • 00:22:55 this directory to start exploring things
  • 00:22:57 and I'll actually let me just do
  • 00:22:58 everything in vs code so let's see I'll
  • 00:23:02 have the terminal in here yes good and
  • 00:23:05 make it bigger so if you have vs code
  • 00:23:11 you can use the terminal built in
  • 00:23:13 terminal otherwise you can still use the
  • 00:23:16 other terminal so I'm gonna do is I'm
  • 00:23:18 gonna create a file in here and call
  • 00:23:20 this file index dot JSP things in index
  • 00:23:24 Tijs so don't worry about yes lint for
  • 00:23:28 now so in here you can do anything that
  • 00:23:31 we just did so you can do math dot
  • 00:23:33 random right and now math at random and
  • 00:23:37 you can execute this file however it
  • 00:23:40 will not output anything because it just
  • 00:23:42 executed executed a value so this is not
  • 00:23:45 rebel node so if we execute node index
  • 00:23:47 that yes it will do nothing output
  • 00:23:49 nothing because we're not a rebel mode
  • 00:23:51 now we're in script execution mode which
  • 00:23:54 means if you want to output something to
  • 00:23:57 the operating system the operating
  • 00:23:59 system that's running the process then
  • 00:24:01 you have to use the interface to the
  • 00:24:03 operating system for example you can do
  • 00:24:05 process so process the STD out don't
  • 00:24:09 worry about the syntax dot right and in
  • 00:24:12 here you would do math dot random and it
  • 00:24:15 will probably not work because right
  • 00:24:17 expects a string so let's try that I
  • 00:24:21 think it will not work
  • 00:24:22 yep
  • 00:24:23 did not work because right here expects
  • 00:24:26 a string so what do you do well you can
  • 00:24:28 change this to two string so in here
  • 00:24:30 what I did is JavaScript this is a
  • 00:24:33 JavaScript expression I just made it
  • 00:24:35 interesting let's make sure this is
  • 00:24:36 working
  • 00:24:37 yep this works so this is a JavaScript
  • 00:24:39 expression right and I fed it to a node
  • 00:24:43 API so this is a node API process the
  • 00:24:46 STD I'll write is a node API now of
  • 00:24:50 course this is very raw a you know lower
  • 00:24:54 level because you have the console
  • 00:24:56 object the console object is also
  • 00:24:58 supported so I can do math dot random
  • 00:25:00 through the console object and that is
  • 00:25:03 going to output it as well and then it's
  • 00:25:06 a lot easier I didn't have to cast it
  • 00:25:08 into a string I did not have to put a
  • 00:25:10 new line after the output it actually
  • 00:25:13 colored it differently so console.log is
  • 00:25:16 another node API and actually if you go
  • 00:25:21 find the source code to a console dot
  • 00:25:23 log for node you'll find that it uses
  • 00:25:25 process the STD out there right so there
  • 00:25:27 are many many ways to do things there
  • 00:25:29 are lower-level api's and higher level
  • 00:25:32 API what if you console dot log an
  • 00:25:34 object what if we console the log
  • 00:25:36 process for example process is an object
  • 00:25:39 what's gonna happen well it's gonna it's
  • 00:25:42 gonna print a huge this is the default
  • 00:25:45 behavior for console to log it will
  • 00:25:47 print a huge object and go through all
  • 00:25:50 the nesting so everything that you can
  • 00:25:52 do on not just the first level
  • 00:25:56 properties for process but everything in
  • 00:25:58 it including arrays and all the
  • 00:26:00 environment variables right so this is
  • 00:26:03 not a very handy output what what do you
  • 00:26:05 do to make it handy well you can use
  • 00:26:08 other higher-level api's for example the
  • 00:26:11 dirt one might be a little bit better
  • 00:26:13 let's actually the third one is exactly
  • 00:26:15 the same but the dirt one accepts depth
  • 00:26:18 I think or like it accepts an object and
  • 00:26:23 in here you can say depth is zero I
  • 00:26:26 think so let's try that yeah so it gave
  • 00:26:29 me only the first level so without any
  • 00:26:32 nesting what is the first level of
  • 00:26:35 properties that you have on
  • 00:26:37 process object so this is a cool trick
  • 00:26:39 where you can start exploring so it did
  • 00:26:42 not expand the big things that you have
  • 00:26:44 in process so this is me executing a
  • 00:26:47 script right so we're gonna talk about
  • 00:26:49 how node does this magic how does it
  • 00:26:52 execute a script can you turn on the
  • 00:26:55 buggin for all libraries using no debug
  • 00:26:57 I don't think so I think you can I have
  • 00:27:00 a comma separated list so you can do
  • 00:27:03 like FS and paths but usually you want
  • 00:27:07 to debug one library because if you turn
  • 00:27:09 on everything there will be thousands
  • 00:27:11 and thousands of debugging lines and
  • 00:27:13 it's not really helpful alright so I am
  • 00:27:17 going to ask you a question and let's
  • 00:27:20 pretend that we are in an interview and
  • 00:27:22 I'm interviewing you for an old position
  • 00:27:26 so I'm gonna ask you an interview
  • 00:27:28 question I am gonna so you know the
  • 00:27:32 about the you know function X right and
  • 00:27:37 function X and we're gonna call it and
  • 00:27:41 function X is gonna console.log
  • 00:27:46 arguments so arguments is a special
  • 00:27:48 keyword that present everything that you
  • 00:27:51 pass to X as an argument right so let's
  • 00:27:53 pass X three arguments so one seven and
  • 00:27:57 nine so what is arguments in this case
  • 00:27:59 if I execute this code it will be one
  • 00:28:02 seven and nine now you'll notice that
  • 00:28:04 arguments looks like an object not an
  • 00:28:07 array that's because arrays in
  • 00:28:09 JavaScript are just objects they're just
  • 00:28:11 special objects where the properties are
  • 00:28:14 numeric just like that so it is this is
  • 00:28:16 an array all right cool so you
  • 00:28:18 understand that right now my question is
  • 00:28:20 what is gonna happen if I just
  • 00:28:24 console.log arguments without any
  • 00:28:26 function what is the output in this case
  • 00:28:28 so that's your first interview question
  • 00:28:29 tell me what is the output in this case
  • 00:28:31 and if your answer is going to be
  • 00:28:37 undefined you're gonna be surprised
  • 00:28:41 arguments in this case is another array
  • 00:28:44 there is an array here with how many
  • 00:28:46 arguments with five exact arguments
  • 00:28:51 so what are these five arguments and why
  • 00:28:54 is arguments working here arguments is
  • 00:28:57 something that you get in a function
  • 00:28:59 right why is it working top level not
  • 00:29:02 what we're not in a function and what
  • 00:29:04 are these five arguments that you get
  • 00:29:06 this is core knowledge in node so the
  • 00:29:10 reason arguments worked is because node
  • 00:29:13 has this concept of wrapping wrapping
  • 00:29:16 modules right so this is what it does it
  • 00:29:19 wraps your code in a function so you get
  • 00:29:22 this if e by default you everything you
  • 00:29:24 do in node gets this if e if he is
  • 00:29:29 immediately invoked function expression
  • 00:29:31 immediately invoked function expression
  • 00:29:33 so you get this if e by default no just
  • 00:29:36 does this wrapping automatically and it
  • 00:29:40 also does other stuffs for example it
  • 00:29:43 returns something from your from your
  • 00:29:45 file we're gonna talk about that but
  • 00:29:48 just in general I want you to know that
  • 00:29:49 you get this if e by default this is why
  • 00:29:52 arguments work so kind of like this no
  • 00:29:55 it has this Fe and it executed it
  • 00:29:56 immediately that's what you get and the
  • 00:30:00 arguments that you get are mod one of
  • 00:30:06 them is module one of them is exports
  • 00:30:08 one of them is a file name and learning
  • 00:30:13 so let's actually take a look at them
  • 00:30:16 real quick I think I'm missing one yeah
  • 00:30:19 so require is the first one what exports
  • 00:30:22 is the first one yeah exports is the
  • 00:30:25 first one I think and then you get
  • 00:30:28 required and then you get module exports
  • 00:30:32 is the first one then acquire then
  • 00:30:34 module you can actually take a look at
  • 00:30:36 them if you just do
  • 00:30:37 if you require module you can take a
  • 00:30:41 look at the wrapper require module dot
  • 00:30:51 wrapper you see that require module dot
  • 00:30:53 wrapper
  • 00:30:56 require module dot wrapper so this is
  • 00:30:58 exactly what you get exports require
  • 00:31:00 module filename and earning this is the
  • 00:31:03 actual wrapper that you know uses it's
  • 00:31:06 exactly that all right cool
  • 00:31:08 so here's the thing if you if you have
  • 00:31:13 two files right so we're gonna have
  • 00:31:15 another file in here index 1 and index 2
  • 00:31:18 index 1 and index 2 GS and I'm just
  • 00:31:22 gonna split them split down alright so
  • 00:31:25 you've got index 1 and index 2 right
  • 00:31:28 what I want you to know is that every
  • 00:31:31 function every file that you initialize
  • 00:31:33 gets this magic Fe right so if I if I
  • 00:31:39 constant log exports in here
  • 00:31:41 right experts is something that you get
  • 00:31:43 from the arguments right and I
  • 00:31:45 console.log it in here these exports are
  • 00:31:48 different right they're not the exact
  • 00:31:50 same object although if you execute them
  • 00:31:54 they're exactly the same it's an empty
  • 00:31:56 object right but this empty object Oh
  • 00:32:00 index to save this empty object that you
  • 00:32:04 get in index to analytics 1 these are
  • 00:32:06 different objects they're not exactly
  • 00:32:08 the same object why because they're
  • 00:32:10 coming from an argument that no passes
  • 00:32:12 to your iffy function so that's
  • 00:32:14 important so all of them are different
  • 00:32:15 right exports require module file name
  • 00:32:18 and their name file name is of course
  • 00:32:19 the name of the file that you're
  • 00:32:21 executing and their name is the name of
  • 00:32:23 the directory where your file is that
  • 00:32:25 you're executing so you need to learn
  • 00:32:29 about exports and and acquired these are
  • 00:32:33 two basic things in in node this is the
  • 00:32:35 node you know module dependency system
  • 00:32:39 so next we're gonna talk about these two
  • 00:32:41 in rebel mode you don't have arguments
  • 00:32:45 because you're not really executing a
  • 00:32:47 file this is when you execute a file
  • 00:32:49 this is how node manages files and
  • 00:32:51 modules
  • 00:32:53 how is console.log working when you run
  • 00:32:55 node file name as console.log is
  • 00:32:57 available only in browser yes console
  • 00:32:59 lock is available in node so node tries
  • 00:33:02 to mirror the browser's API right so
  • 00:33:05 when it comes to input and output not
  • 00:33:09 so there's no time you can't do document
  • 00:33:11 you can't in here you can't say oh look
  • 00:33:14 anywhere really you can't say document
  • 00:33:19 document dot get element by ID that is
  • 00:33:22 not going to work why because you don't
  • 00:33:24 have Dom so this will tell you undefined
  • 00:33:27 some kind of error document is not
  • 00:33:30 defined right so it's not exactly the
  • 00:33:32 browser however node opted to implement
  • 00:33:36 things like console like set timeout and
  • 00:33:41 many and a few other things that exist
  • 00:33:43 in a browser environment but they're not
  • 00:33:45 JavaScript right so these api's are
  • 00:33:49 built in node and their implementation
  • 00:33:52 is different than the implementation of
  • 00:33:53 the browser so and v8 has nothing to do
  • 00:33:58 with them v8 is just gonna organize the
  • 00:34:01 execution of functions and callbacks but
  • 00:34:03 these these api's are not– cool that's
  • 00:34:08 why so if you go grab the source code of
  • 00:34:11 node you're gonna find the console
  • 00:34:13 object and a lot function in the node
  • 00:34:15 source code right not in v8 and not for
  • 00:34:19 any browsers okay cool no questions keep
  • 00:34:21 them coming
  • 00:34:22 so we're look we're learning about this
  • 00:34:26 iffy function if he wrapping function
  • 00:34:30 right cool so what is exports exports is
  • 00:34:33 an object that you can change so exports
  • 00:34:37 is a mutable object so we can say
  • 00:34:39 exports dot answer is 42 and you can put
  • 00:34:44 any number of properties on exports and
  • 00:34:47 when we execute this file exports is
  • 00:34:49 gonna be whatever you define it so it's
  • 00:34:52 just a special object that is available
  • 00:34:54 globally for you it's also an alias to
  • 00:34:59 module duck exports so if I do module
  • 00:35:02 the exports the another answer then I am
  • 00:35:07 changing the exact same property so you
  • 00:35:11 see how I changed it here and I change
  • 00:35:12 it it bear so exports is just an alias
  • 00:35:15 to module the exports cool and here's
  • 00:35:17 the thing node when it manages a file it
  • 00:35:21 returned
  • 00:35:21 module exports so whatever you put on
  • 00:35:25 exports that would be the return value
  • 00:35:28 of the index file the index module now
  • 00:35:32 why is this handy because another file
  • 00:35:35 might require your module so let's call
  • 00:35:39 this module one this index is your
  • 00:35:41 module right and you want index two to
  • 00:35:44 use your module to use index what you do
  • 00:35:46 is you require you just use require and
  • 00:35:50 you pass in a string and you say index
  • 00:35:53 two is gonna require index one
  • 00:35:55 Dodge's just like that index two is
  • 00:35:58 gonna require in this one so note what's
  • 00:36:02 gonna happen and in here I'm not gonna
  • 00:36:07 console.log I'm just gonna console.log
  • 00:36:09 let's console.log in index dot yes right
  • 00:36:15 let's pass a lot of an index digest so
  • 00:36:17 just by requiring index if I execute
  • 00:36:20 index two what can happen is note is
  • 00:36:24 going to look at this find require and
  • 00:36:26 it will require this file so it will
  • 00:36:28 execute this file so I'm gonna get the
  • 00:36:30 console log message although I'm
  • 00:36:32 executing index two so that's that's the
  • 00:36:35 story of require but here's how it's
  • 00:36:38 helpful it's helpful because require
  • 00:36:40 gives you access to whatever this module
  • 00:36:44 the export things is returning so
  • 00:36:46 whatever you put in module the exports
  • 00:36:48 you get it in here so in here you get an
  • 00:36:51 object index I'm just gonna call it
  • 00:36:55 index and I'm just gonna log index so
  • 00:36:58 guess what index is going to be index
  • 00:37:00 after index that in in index digest this
  • 00:37:03 here you get the exported module so this
  • 00:37:09 is module two exports so whatever you
  • 00:37:11 put on module the exports or its alias
  • 00:37:13 exports you get it out when you require
  • 00:37:16 a file so require is dependencies module
  • 00:37:19 dependencies it's similar to import and
  • 00:37:22 export in JavaScript in fact you can
  • 00:37:25 also use import and export and I'm going
  • 00:37:27 to show you how that because this is new
  • 00:37:29 this is new in node you get to use
  • 00:37:31 exports and imports all so any questions
  • 00:37:34 about
  • 00:37:35 and this exports business because that
  • 00:37:38 is that is core knowledge all all the
  • 00:37:41 packages out there that are written for
  • 00:37:44 note they use this syntax they use
  • 00:37:47 exports and require all right cool so
  • 00:37:53 I'm going to clean all this and just
  • 00:37:56 console.log hello and in here
  • 00:38:00 I am going to require index that yes and
  • 00:38:04 I'm gonna execute the file that's
  • 00:38:06 requiring so this will output hello
  • 00:38:09 right cool here is your second interview
  • 00:38:14 question what's gonna happen if I do
  • 00:38:16 that if I require index three times
  • 00:38:21 what's gonna happen so go ahead and try
  • 00:38:25 and answer this will output hello one
  • 00:38:29 time what why the output hello one time
  • 00:38:32 and not three times right and the reason
  • 00:38:36 is note has this required function in
  • 00:38:39 node has some magic caching it doesn't
  • 00:38:43 evaluate the file every time you require
  • 00:38:46 it it evaluated once and then the second
  • 00:38:50 time it reads it from the cache so this
  • 00:38:54 is why in a in an application say to
  • 00:38:56 react application or an angular
  • 00:38:58 application you require react all over
  • 00:39:01 the place you require angular all over
  • 00:39:03 the place multiple files are gonna
  • 00:39:05 require react or angular or view and
  • 00:39:07 only the first require is gonna actually
  • 00:39:11 do the work the second require will just
  • 00:39:12 read it from the cache so well another
  • 00:39:16 interview question is what if I do want
  • 00:39:19 this file to console.log hello every
  • 00:39:22 time I require it and the answer is you
  • 00:39:26 really can't you require you get cash
  • 00:39:29 that's just that's just the reality
  • 00:39:32 however you can delay what you execute
  • 00:39:35 in the file by making it return a
  • 00:39:39 function
  • 00:39:39 so remember module to export right
  • 00:39:41 module the export is our API so instead
  • 00:39:45 of having the
  • 00:39:48 console.log directly you can't have it
  • 00:39:51 export a closure you can have an export
  • 00:39:53 a function closure and you execute that
  • 00:39:56 function every time you want it so if I
  • 00:39:58 make module de exports into a function
  • 00:40:02 and I'm gonna use error function in here
  • 00:40:04 but you can just use a regular function
  • 00:40:06 and I make this function console.log
  • 00:40:08 hello instead of that now I have delayed
  • 00:40:11 execution now every time I require index
  • 00:40:15 dot yes
  • 00:40:15 what do I get every time I require index
  • 00:40:18 suggest I get back a function it does
  • 00:40:21 not have any code it will just cache the
  • 00:40:23 function reference and I can just
  • 00:40:26 execute this function that I get back
  • 00:40:28 from index that yes so if I do that and
  • 00:40:31 I execute this I get hello three times
  • 00:40:34 so we use this trick a lot in node we
  • 00:40:37 make Lee module exports a function and
  • 00:40:40 we execute the code in that function so
  • 00:40:43 it's a little bit of delayed execution
  • 00:40:46 what gets cached is the function itself
  • 00:40:48 not the function execution cool alright
  • 00:40:52 so there are many cases where you get me
  • 00:40:56 you want to execute something in a file
  • 00:40:58 multiple times right so this is how you
  • 00:41:02 do it you always execute a function in
  • 00:41:04 fact if you need the file to have
  • 00:41:06 multiple capabilities what you do is you
  • 00:41:10 make it export an object and in the
  • 00:41:13 object you have your capabilities you do
  • 00:41:15 like function one function to function
  • 00:41:17 three right so this is another way to
  • 00:41:20 export API so how would you do that
  • 00:41:22 how would you invoke function one now
  • 00:41:25 what you get back is an object right
  • 00:41:27 it's not a function it's an object and
  • 00:41:29 because it's an object it's an object
  • 00:41:31 that has F one I can do dot F one right
  • 00:41:35 dot F one because it's an object and
  • 00:41:37 execute F one right and this will work
  • 00:41:40 as well so whatever you put in here this
  • 00:41:45 require is just gonna use it right so
  • 00:41:47 and you can put anything right you can
  • 00:41:49 put primitive values you can put arrays
  • 00:41:51 objects or you can have like a proxy
  • 00:41:54 anything that is supported by the
  • 00:41:56 language you can use it as the exports
  • 00:41:59 value in your
  • 00:42:01 and you get to use it exactly as we
  • 00:42:04 define it right whatever you have we
  • 00:42:05 have an object has an f1 property so
  • 00:42:08 I'll just cut like get the f1 property
  • 00:42:11 and then because the f1 property itself
  • 00:42:13 is a function I can execute f1 right so
  • 00:42:17 there's nothing magic in here it's just
  • 00:42:18 JavaScript mixed with the API that no
  • 00:42:22 gives you to require functions now this
  • 00:42:25 is a little bit different than the
  • 00:42:26 import-export syntax that is that is
  • 00:42:29 native to the JavaScript language right
  • 00:42:31 this module require business is nodes
  • 00:42:34 way to do module dependency and it
  • 00:42:37 existed since day one in node so like
  • 00:42:40 ten years before JavaScript decided to
  • 00:42:42 have a module dependency as well now the
  • 00:42:44 problem is you have two systems one has
  • 00:42:47 been used for ten years and the
  • 00:42:50 brand-new one that everybody is gonna
  • 00:42:52 start using because it works in browsers
  • 00:42:54 as well
  • 00:42:56 so what do you do well here's what you
  • 00:42:59 do you really understand require module
  • 00:43:01 to export as to understand it really
  • 00:43:03 good because you're gonna have to deal
  • 00:43:04 with packages that are written in this
  • 00:43:06 sentence for at least five to ten years
  • 00:43:09 into the future if you're working in
  • 00:43:11 node project but in your own parting a
  • 00:43:13 node project today I would recommend
  • 00:43:15 that you use the import/export syntax
  • 00:43:17 not the required syntax now they have
  • 00:43:21 different implementation and I'm gonna
  • 00:43:24 tell you about that in just a little bit
  • 00:43:26 just so that you aware but I'm going to
  • 00:43:28 show you the import-export n tax first
  • 00:43:30 so let's go ahead and do that so let's
  • 00:43:33 do this exact same example but instead
  • 00:43:35 of working with require let's do it with
  • 00:43:39 with important export right so here's
  • 00:43:44 the thing if you have node twelve right
  • 00:43:47 if you have node twelve you can use
  • 00:43:49 import and export out of the box it's
  • 00:43:51 actually supported but remember that
  • 00:43:53 it's not LTS so no twelve is not
  • 00:43:56 Althea's yet
  • 00:43:57 what's the MV m LS system no twelve
  • 00:44:03 so nvm use system
  • 00:44:09 MVM LS remote while we're talking about
  • 00:44:14 it I'm just gonna get the latest and
  • 00:44:15 greatest node which is V 1214 and I'll
  • 00:44:18 use that so MDM install this thing and
  • 00:44:23 this will install the latest so what do
  • 00:44:25 you do here is you create a package that
  • 00:44:27 JSON file to start managing your
  • 00:44:30 dependencies right let's actually name
  • 00:44:33 this m1 and m2 because they're kind of
  • 00:44:37 different so m1 and m2 and I'm just
  • 00:44:40 gonna call em two in here m1 m1 that is
  • 00:44:45 N 1 and n 2 so exact same example I just
  • 00:44:47 named them in m1 and m2 instead of what
  • 00:44:51 I had before all right and and you can
  • 00:44:56 run them with the latest node so this
  • 00:44:58 syntax is not going anywhere so although
  • 00:45:01 we have node V 12 now we can still run
  • 00:45:04 m2 and we get the exact same behavior so
  • 00:45:07 export and require is exactly the same
  • 00:45:09 but what I'm to do is I'm gonna change
  • 00:45:11 this into important export but to do
  • 00:45:14 that I need the package JSON file
  • 00:45:17 so what is a package that JSON file a
  • 00:45:19 package that JSON file is the file that
  • 00:45:22 you can use to manage meta information
  • 00:45:25 about your node projects so every node
  • 00:45:28 package out there has a package that
  • 00:45:31 JSON file to tell you its name its
  • 00:45:34 version its get repository how to run
  • 00:45:37 things in it so you create a package
  • 00:45:39 that JSON file with the NPM command
  • 00:45:42 so what NPM is the node package manager
  • 00:45:44 and we're going to talk about it in the
  • 00:45:46 next hour after the break but for now
  • 00:45:49 the first step is to use the NPM init
  • 00:45:52 command to create a package dot JSON
  • 00:45:55 file that we can use to control things
  • 00:45:58 in nodes so @p a minute and this will
  • 00:46:01 ask you a few questions us or it can
  • 00:46:05 detect if you do – Y which is for yes it
  • 00:46:08 says just try to detect whatever you can
  • 00:46:10 detect from this this object and just
  • 00:46:12 give me a package JSON file so this will
  • 00:46:15 create a package of the JSON file using
  • 00:46:17 the name of the directory using a
  • 00:46:19 default version it will try to find if
  • 00:46:20 you have git installed it will
  • 00:46:23 to find the get remote that you have it
  • 00:46:25 just detects a few things so in package
  • 00:46:28 that Jason you get to specify
  • 00:46:31 information about your package so for
  • 00:46:34 example if you put your name on the
  • 00:46:35 package so that when you push it people
  • 00:46:37 know if you want to have keywords so
  • 00:46:39 people can find your package in the
  • 00:46:41 search you can use this meta information
  • 00:46:44 so there is a new by the way the only
  • 00:46:49 two required properties in package that
  • 00:46:52 jason are just name and version these
  • 00:46:53 are the only required everything else is
  • 00:46:55 optional so the new the new property
  • 00:46:59 that you can use here on package that
  • 00:47:01 jason is the type property and if you
  • 00:47:05 specify your type as module instead of
  • 00:47:08 commonjs
  • 00:47:09 so the other one I think is common
  • 00:47:10 common jazz which is the name that is
  • 00:47:13 that we use for you know the require
  • 00:47:16 exports thing but if you specify your
  • 00:47:19 type as module then node will switch
  • 00:47:23 gears and just assume that you're just
  • 00:47:26 gonna use your ECMO script modules all
  • 00:47:29 over the place so there's no require and
  • 00:47:31 exports in fact I think if we try to
  • 00:47:33 execute it now it will not execute it
  • 00:47:36 will not be happy it will say well I
  • 00:47:39 actually executed that does not work oh
  • 00:47:42 we need a flag so we need the
  • 00:47:46 experimental modules there you go see
  • 00:47:49 so this flag is actually gonna go away
  • 00:47:51 when node goes into LTL I'm not sure
  • 00:47:55 when they're gonna remove it but they
  • 00:47:56 will remove it but this is this is how
  • 00:47:58 node is gonna behave when it switches to
  • 00:48:00 the ECMO script the ASM module loader
  • 00:48:02 see this is now the ESM module loader so
  • 00:48:05 it tells you I don't know require
  • 00:48:07 because I'm assuming that you are in um
  • 00:48:09 in a neck massager
  • 00:48:11 right you're not in a node module you're
  • 00:48:14 an ECMO script module which means you
  • 00:48:15 cannot use exports you cannot use
  • 00:48:17 require so what can you use you use
  • 00:48:20 export default and in here you pass in
  • 00:48:24 the object that you'd like to export so
  • 00:48:26 it's kind of similar to what you did but
  • 00:48:28 in here where we have a little bit of
  • 00:48:31 different syntax export default so
  • 00:48:33 instead of module de exports you do
  • 00:48:35 export default
  • 00:48:36 and instead of require what you do is
  • 00:48:38 you import you use the import keyword so
  • 00:48:41 import m1 from dot slash m1 dot yes well
  • 00:48:47 import m1 from the slash in Monda yes
  • 00:48:49 and now you get to do m1 dot f1 and
  • 00:48:53 execute that three times so you'll see a
  • 00:48:56 little bit of different syntax but it's
  • 00:49:02 basically the same module dependency so
  • 00:49:04 this works right you get a warning that
  • 00:49:06 it is experimental but it's really just
  • 00:49:08 it works fine they're just making sure
  • 00:49:12 that the API is stable so it is the
  • 00:49:14 exact same thing except there's no
  • 00:49:16 require there's no module the expert
  • 00:49:18 cool now we have a big question
  • 00:49:21 what is a module so that is an important
  • 00:49:24 question what is a module so I'm gonna
  • 00:49:26 give you the short answer and I'll give
  • 00:49:28 you the long answer after the break
  • 00:49:30 so the short answer is a module is just
  • 00:49:33 a file it's just a bunch of code lines
  • 00:49:36 of code in a file but it can also be
  • 00:49:38 multiple files right and we get to we
  • 00:49:43 now we need to understand what happens
  • 00:49:45 when you require or import a module I'll
  • 00:49:48 just I'll just use import in here what's
  • 00:49:51 gonna happen when you import not a local
  • 00:49:54 file so in here what we imported is a
  • 00:49:56 local file right and one what happens if
  • 00:49:59 you import say HTTP from HTTP what
  • 00:50:06 happens if you do that because this is
  • 00:50:08 not local because there is no even this
  • 00:50:11 this dot slash means a file on the same
  • 00:50:14 level trying to file a find a file on
  • 00:50:17 the same level or a folder on the same
  • 00:50:18 level so if you don't have duck slash or
  • 00:50:20 dot dot slash that means go find this in
  • 00:50:24 a special somewhere and this special
  • 00:50:27 somewhere is what we're going to talk
  • 00:50:28 about next is called the node modules
  • 00:50:30 directory and I want to tell you how
  • 00:50:32 node modules tie in to NPM and other
  • 00:50:36 package managers and how things work
  • 00:50:38 magically now HTTP is a built in module
  • 00:50:42 so it doesn't really go and look for it
  • 00:50:44 in the node modules directory it just
  • 00:50:46 take it from the built-in core the
  • 00:50:50 comes installed out-of-the-box with
  • 00:50:51 known but if you do import from Express
  • 00:50:54 for example which is a package that you
  • 00:50:57 can use to replace hdtb then you need to
  • 00:51:00 have Express installed this is not a
  • 00:51:02 third-party package they said this is
  • 00:51:04 not a built-in package it's a
  • 00:51:05 third-party package so you have to
  • 00:51:08 install it you have to let NPM install
  • 00:51:09 this before you can use it so we'll do
  • 00:51:12 that right after the break so can we
  • 00:51:16 have a library of components and import
  • 00:51:20 it as a module for another project
  • 00:51:22 definitely the answer is big yes that's
  • 00:51:25 how node works in fact when we import
  • 00:51:27 HTTP what we're importing is a small
  • 00:51:30 library of things that we can use and
  • 00:51:33 you can explore this right away by
  • 00:51:37 console logging HTTP so let's take a
  • 00:51:38 look at what we get when we import HTTP
  • 00:51:41 and in here we're just gonna execute m1
  • 00:51:43 format from now on m1 and you get a
  • 00:51:46 library a little library object with a
  • 00:51:49 few methods so there is status codes for
  • 00:51:54 example all the HTTP status codes are
  • 00:51:55 here so if you want to look them up all
  • 00:51:57 you need to do is HTTP status codes you
  • 00:52:01 get a few important methods and and
  • 00:52:05 objects and classes in here and the most
  • 00:52:08 important one is create server so HTTP
  • 00:52:11 that create server creates an HTTP
  • 00:52:13 server that's what we're gonna use next
  • 00:52:15 if you have node 10 this is not going to
  • 00:52:20 work what we're doing here import HDB is
  • 00:52:22 not going to work so you have two
  • 00:52:24 options either upgrade to node 12 and
  • 00:52:26 use that or you can use the ESM package
  • 00:52:30 and this is how you do it you add cam
  • 00:52:32 installed ESM and this will download ESM
  • 00:52:38 from the internet and place it under a
  • 00:52:41 node modules folder so we're going to
  • 00:52:43 talk about that in just a little bit a
  • 00:52:45 place ESM under a node modules folder
  • 00:52:47 you see that and what you what you need
  • 00:52:50 to do to run things with the sm is
  • 00:52:52 instead of experimental modules you do –
  • 00:52:55 or yes em – are yes i'm so on more time
  • 00:53:00 – re sm so this will work
  • 00:53:03 on node ten and eight and everything is
  • 00:53:06 just a shim around experimental module
  • 00:53:10 so you don't have you don't need no
  • 00:53:11 twelve and it is it works actually
  • 00:53:14 really good so that's our ESM so two new
  • 00:53:18 things just happen we use the – our flag
  • 00:53:21 what is – our so – our is you require a
  • 00:53:27 module before you execute your code so
  • 00:53:29 that's our is kind of like a wrapper
  • 00:53:31 that you can inject a module into your
  • 00:53:34 system before node executes your actual
  • 00:53:37 module which is m1 degeus so that that's
  • 00:53:41 that's our and ESM is just a package
  • 00:53:44 that we downloaded so we can now import
  • 00:53:47 ESM from node modules so before we talk
  • 00:53:50 about HTTP I wanna I want to talk about
  • 00:53:52 node modules right so if you import
  • 00:53:56 let's say you want to import from like
  • 00:53:59 or just import and you pass in something
  • 00:54:04 you import something right what happens
  • 00:54:08 here is when you put something
  • 00:54:10 what node looks at something here
  • 00:54:14 there's something string and it figures
  • 00:54:16 out that this is not a built-in library
  • 00:54:19 right it's not part of the built-in
  • 00:54:21 library it's also not a local file
  • 00:54:24 because you know it doesn't start with
  • 00:54:27 dot slash or dot dot slash so if it's
  • 00:54:29 start with dot slash or dot dot slash
  • 00:54:32 that means it's a local file go find it
  • 00:54:35 locally where we're importing but
  • 00:54:37 because it does not start with dot or
  • 00:54:39 dot dot slash that means it's not a
  • 00:54:41 local file so know by default will tell
  • 00:54:44 you that I don't know what something is
  • 00:54:46 cannot find module something because it
  • 00:54:49 looks under node modules so it tries to
  • 00:54:53 find something dot yes under node
  • 00:54:56 modules so if we go to your node modules
  • 00:54:58 and we put something dot yes and we and
  • 00:55:03 we do something we do and we put
  • 00:55:05 anything here in digest so this is just
  • 00:55:09 a test so what I did is I put something
  • 00:55:12 just under node modules and guess what
  • 00:55:15 now
  • 00:55:16 I am importing something and there is
  • 00:55:18 something that is under node modules it
  • 00:55:20 will find it so there's no magic here it
  • 00:55:23 just looks for things under node modules
  • 00:55:25 that's that's what node does right and
  • 00:55:28 you can actually make this into a folder
  • 00:55:31 instead of a file so you can have a
  • 00:55:33 folder called something under node
  • 00:55:35 modules you can make a folder and name
  • 00:55:37 it something and put your code under
  • 00:55:41 this folder and by default node looks
  • 00:55:43 for index dot yes under the folder so
  • 00:55:46 just a test here from a folder right and
  • 00:55:50 you and if you import something here
  • 00:55:54 right you I can import the folder and it
  • 00:55:57 will look for the index that you has
  • 00:55:59 file under that folder and that's
  • 00:56:01 exactly how note resolved your external
  • 00:56:04 dependencies so if we npm install lodash
  • 00:56:11 for example if we npm install lo – hello
  • 00:56:14 – is a javascript library that you can
  • 00:56:16 use it has a lot of cool utility
  • 00:56:19 functions that you can use in in our
  • 00:56:21 missing JavaScript you can use them
  • 00:56:23 so what MPM does is it just places let's
  • 00:56:28 refresh this it places lo – just like I
  • 00:56:32 did for something it places lo – under
  • 00:56:34 your nose modules directory that's what
  • 00:56:36 IPM does it has nothing to do with
  • 00:56:37 import and export it just download slow
  • 00:56:40 – and put it under the node modules
  • 00:56:42 folder in your project then here we can
  • 00:56:45 import lo – because it is no module it
  • 00:56:50 is under node modules then it is
  • 00:56:51 something that I can just import and
  • 00:56:54 import directly means just execute so
  • 00:56:57 I'm not really interested in anything
  • 00:56:59 that the library returns like whatever
  • 00:57:01 the library exports right but if you
  • 00:57:03 want to not use whatever the library
  • 00:57:06 exports and you have to give it a name
  • 00:57:07 usually lo – we do import underscore
  • 00:57:09 from lo – and now we get to do use the
  • 00:57:13 lodash functions for example we can sum
  • 00:57:15 in the range so this is one two three
  • 00:57:17 and it will summon array of course we
  • 00:57:20 need to console.log that so you want to
  • 00:57:23 put it in a console log m1 and it is not
  • 00:57:27 working is it at all
  • 00:57:30 get it put it in a race it doesn't some
  • 00:57:32 an argument it will summon irate okay
  • 00:57:35 cool so this is how we use external
  • 00:57:38 libraries we use NPM to download them
  • 00:57:41 and place them under the node modules
  • 00:57:43 folder then we use node package
  • 00:57:47 management to import them into the
  • 00:57:49 project and use their API in in the
  • 00:57:51 project that's how that's the story of
  • 00:57:54 node packages and how to use them in
  • 00:57:57 general how do I make my library
  • 00:58:00 available for people to use it as
  • 00:58:02 imports in their project very good
  • 00:58:04 question
  • 00:58:05 what if our something that yes is turn
  • 00:58:08 turns out to be handy and we want to
  • 00:58:11 make it available for other people to
  • 00:58:13 use it what we do is we publish it under
  • 00:58:16 NPM now or we publish it under a
  • 00:58:19 repository in general right but if I
  • 00:58:22 want to publish my something that you
  • 00:58:24 ask all I need to do is I need to help
  • 00:58:28 make it have a package.json and then I
  • 00:58:30 can just NPM pop lish this this thing
  • 00:58:34 NPM publish now to publish NPM you have
  • 00:58:37 to have an account with NPM and NPM GS
  • 00:58:40 comm right and it has to be a valid
  • 00:58:43 package and you have to have the right
  • 00:58:45 to push through this package if it's a
  • 00:58:47 new package if NPM doesn't have a
  • 00:58:49 package named something already so the
  • 00:58:51 name is unique then it will allow you to
  • 00:58:54 push it but if you're trying to push
  • 00:58:56 through a package that exists you need
  • 00:58:57 to have rights to it so they need to add
  • 00:59:00 you as a contributor to that package and
  • 00:59:02 of course you can publish to github
  • 00:59:04 directly github in fact if we look for
  • 00:59:09 lodash in here
  • 00:59:12 github low – low – right you can
  • 00:59:15 actually install low – directly from
  • 00:59:18 github or let's let's let's talk about
  • 00:59:21 Express so I'm gonna I'm gonna do that
  • 00:59:22 for you next Express not here Express
  • 00:59:29 Express is a framework a web framework
  • 00:59:33 that you can use to replace STD so we're
  • 00:59:36 I'm gonna install Express from github
  • 00:59:39 directly so the minute you push
  • 00:59:41 something to github it's
  • 00:59:44 available for people to use it with
  • 00:59:46 their node projects so let's use let's
  • 00:59:48 MPM Express from github so when I do in
  • 00:59:53 here NPM install Express just like that
  • 00:59:58 that will install it from the NPM
  • 01:00:01 registry it will not install it from
  • 01:00:02 anywhere else because I'm doing NPM
  • 01:00:04 install the default is the NPM registry
  • 01:00:07 but if I use this expression here which
  • 01:00:11 is you know just Express the GS /
  • 01:00:13 Express instead of Express NPM will
  • 01:00:17 detect that you mean go get express from
  • 01:00:20 github by default this expression means
  • 01:00:23 oh this looks like you know older slash
  • 01:00:26 repository so it must be from github so
  • 01:00:30 what it what exactly does it do it will
  • 01:00:32 get the exact latest commits the one
  • 01:00:34 that happens 25 days ago so it will give
  • 01:00:37 you the latest and greatest of Express
  • 01:00:39 so it's not the published package so the
  • 01:00:42 final published package was in NPM if
  • 01:00:45 you go to NV MGS comm and you look for
  • 01:00:48 Express this has expressed this is this
  • 01:00:52 is the package that you get if you just
  • 01:00:54 install it directly from Express and the
  • 01:00:56 final package was 417 one and it is 25
  • 01:01:00 days ago so it looks like they match but
  • 01:01:02 if someone pushed something to get to
  • 01:01:04 Express today and then you do did NPM
  • 01:01:07 install Express / Express you get the
  • 01:01:09 exact same commits that happened today
  • 01:01:13 not the actual final published package
  • 01:01:16 but this is how easy for you to get
  • 01:01:19 something from github so if you push
  • 01:01:21 your package to github you can get it
  • 01:01:23 github also has a package registry that
  • 01:01:25 they're working on so you can install
  • 01:01:27 things from github directly as well but
  • 01:01:32 the package has to exist exist somewhere
  • 01:01:34 you have to publish it somewhere right
  • 01:01:36 all right cool good questions keep them
  • 01:01:39 coming
  • 01:01:39 how does the repository validate the
  • 01:01:43 authors right to upload very good
  • 01:01:45 question so you'll have to have some
  • 01:01:48 kind of authentication and by default it
  • 01:01:51 asks you for to login so there is you
  • 01:01:53 create an account with NPM j/s and you
  • 01:01:55 login with an email
  • 01:01:57 and password so if you do npm publish
  • 01:01:59 you will ask you for you log in it you
  • 01:02:02 log in and password but you can also use
  • 01:02:03 SSH keys if you want to say you know
  • 01:02:06 just like you push to a github
  • 01:02:07 repository and just like how github
  • 01:02:09 validate that you have commit access to
  • 01:02:12 that repository using a succession
  • 01:02:13 education but you do need to
  • 01:02:15 authenticate somehow and after you
  • 01:02:18 authenticate because they know your
  • 01:02:20 identity they're gonna do the
  • 01:02:21 authorization they're gonna validate in
  • 01:02:23 their system does this person have the
  • 01:02:27 right to push to this repository or to
  • 01:02:29 this package alright great questions
  • 01:02:32 keep them coming so we were talking
  • 01:02:35 about import and export so I think that
  • 01:02:40 is that is basically all the time we
  • 01:02:42 have to talk about node modules and
  • 01:02:44 import and export because we need to
  • 01:02:46 explore other things in in the node
  • 01:02:47 ecosystem so if you have any questions
  • 01:02:50 about what we did so far before we move
  • 01:02:52 on about all the import and export stuff
  • 01:02:55 the require the the ACMA script modules
  • 01:02:58 the node modules directory package jason
  • 01:03:02 and how these things work together
  • 01:03:04 definitely now is the time to ask them
  • 01:03:06 but otherwise we're gonna explore the
  • 01:03:09 HTTP package so we're gonna create
  • 01:03:12 certain fact I'm gonna call it server
  • 01:03:14 Dodge's I'm gonna create a server here
  • 01:03:17 so I'll delete everything I'll just keep
  • 01:03:19 server degeus and I don't really need
  • 01:03:22 any of those things so I'm gonna show
  • 01:03:25 you how to get rid of them and then just
  • 01:03:28 delete everything and just keep oh man
  • 01:03:34 we have a lot of stuff that came from
  • 01:03:36 Express but I'll show you how to clean
  • 01:03:38 this up so what I'm gonna do is I'm
  • 01:03:40 gonna create a server not here sorry in
  • 01:03:45 here we're gonna create server JS file
  • 01:03:47 and we're gonna execute the server
  • 01:03:50 digest file so in here we're gonna pass
  • 01:03:52 a log hello node and we're gonna execute
  • 01:03:56 so what are we executing here is just oh
  • 01:03:59 man no it's zero sink yeah all right I'm
  • 01:04:05 gonna just execute no – re SM and server
  • 01:04:09 digest
  • 01:04:10 we're gonna actually create a web server
  • 01:04:12 in here and we're gonna do it first
  • 01:04:14 natively with node and then we'll do it
  • 01:04:16 with Express all right so are we gonna
  • 01:04:18 talk can modules be installed globally
  • 01:04:21 yes so if you want to install a module
  • 01:04:24 globally all you need to do is npm
  • 01:04:26 install dash G so that G flag means
  • 01:04:29 install this module globally for example
  • 01:04:31 I want to have a module to create a
  • 01:04:34 react project or create react app create
  • 01:04:36 react tab is a candidate for something
  • 01:04:40 that you want to have globally because
  • 01:04:42 it just creates a react app for you it's
  • 01:04:45 not really part of a dependency like
  • 01:04:48 lodash or Express you don't depend on it
  • 01:04:50 you just use it to create something
  • 01:04:52 another module that is a good candidate
  • 01:04:56 for a global is es lint you won't have
  • 01:04:58 es lens in all your node projects so
  • 01:05:00 maybe just manage it globally although
  • 01:05:03 it is probably a better idea to manage
  • 01:05:05 things locally even for even es lint
  • 01:05:08 because you want to make sure that the
  • 01:05:10 whole team uses the exact same version
  • 01:05:12 and they don't depend on a global
  • 01:05:15 dependency so I would avoid global
  • 01:05:17 dependencies unless you know it's a
  • 01:05:20 thing that you use globally for
  • 01:05:22 everything it's a command basically
  • 01:05:24 unless it's a command that gives you
  • 01:05:25 about a binary executable command
  • 01:05:28 just use local dependencies so let me
  • 01:05:31 mention a few more things about packaged
  • 01:05:33 on Jason we're limited on time but I'll
  • 01:05:36 talk a little bit more about it so one
  • 01:05:39 thing that you can do is use scripts so
  • 01:05:42 probably the most important part of
  • 01:05:45 package that Jason for me in a big
  • 01:05:46 project is the user scripts in fact if
  • 01:05:49 we go I'm gonna show you a sneak peek
  • 01:05:52 into the package that Jason for the J's
  • 01:05:55 complete project so let's take a look at
  • 01:05:57 the package that JSON this is the Jas
  • 01:06:00 complete calm this is packaged in JSON
  • 01:06:02 for J's complete that form and you'll
  • 01:06:04 see that I have so many things under the
  • 01:06:07 scripts directory see that all of these
  • 01:06:10 are a tasks that we use to run things
  • 01:06:13 J's complete some of them are
  • 01:06:15 development tasks so things that we do
  • 01:06:17 for development and some of them are
  • 01:06:19 production tasks the things that we do
  • 01:06:21 for production and these are different
  • 01:06:23 so
  • 01:06:24 any any person who uses who works on the
  • 01:06:27 J's complete project they know exactly
  • 01:06:30 how to start a dev process they know
  • 01:06:32 exactly how to bundle things for dev
  • 01:06:34 they know exactly how to send all the
  • 01:06:38 gist drops emails or update all the
  • 01:06:40 email templates or log things in
  • 01:06:42 production so these texts these tasks
  • 01:06:44 are unified let me know we always have
  • 01:06:47 to do it this way so how do we do that
  • 01:06:51 in in our project so let's let's create
  • 01:06:54 a task so for example instead of always
  • 01:06:56 doing node – our ESM server yes right
  • 01:07:00 and if you if you bring someone new to
  • 01:07:03 the team you have to tell them oh you
  • 01:07:05 have to use – or ESM in our project
  • 01:07:07 instead of doing that you can have a
  • 01:07:09 start script you can say you know what
  • 01:07:11 we're gonna put this in a start script
  • 01:07:13 so the start script is node – art ESM
  • 01:07:18 server yes this is how you start our
  • 01:07:21 system and now it's a global knowledge
  • 01:07:23 because all I need to do is NPM start
  • 01:07:25 because I can run the script directly
  • 01:07:28 NPM start I don't have and if you bring
  • 01:07:30 someone new to that table they know they
  • 01:07:33 they know to look for NPM start right
  • 01:07:35 because you know it's a it's a global
  • 01:07:37 language there's what can I do for this
  • 01:07:39 project well you can start it and boom
  • 01:07:41 hello node it is running the – Ari sm
  • 01:07:45 server so you will have a lot more in
  • 01:07:47 here for example what do we do to test
  • 01:07:49 things right maybe are we using mocha
  • 01:07:54 are we using just where where do we look
  • 01:07:56 up the tests so maybe that will be like
  • 01:07:58 oh just just under you know star star
  • 01:08:01 type or whatever things that you do and
  • 01:08:04 now you can do NPM test and it will run
  • 01:08:07 just now I don't have just installed so
  • 01:08:09 this will tell me where what is just I
  • 01:08:11 don't know just but you get the idea
  • 01:08:13 so these are special commands that you
  • 01:08:18 can do with just NPM start and NPM test
  • 01:08:20 but they're equivalent to doing NPM run
  • 01:08:22 start so NPM start is equivalent to
  • 01:08:25 doing NPM run start it's just a special
  • 01:08:28 one that has a shortcut but in general
  • 01:08:30 if you do something like let's say I
  • 01:08:35 want to do it dev start not
  • 01:08:37 not just start or prod how do you start
  • 01:08:39 for production for example how do you
  • 01:08:41 start things for a production well in
  • 01:08:43 production I'm gonna use the I'm gonna
  • 01:08:47 use node in the equal production for
  • 01:08:50 example and then do not know – re SM
  • 01:08:53 server that yes right so I slightly
  • 01:08:56 different to run things in production
  • 01:08:58 then in development right so you have a
  • 01:09:02 different tasks for production run and
  • 01:09:04 if you want to run this task then you
  • 01:09:06 have to use npm run prod start because
  • 01:09:10 it doesn't have a shortcut so at 10 p.m.
  • 01:09:12 run prod script in fact these things are
  • 01:09:14 called run scripts because he uses in
  • 01:09:16 with npm run cool you can also use yard
  • 01:09:20 so if you're using the yarn yarn is
  • 01:09:23 gonna run into yarn will funny so you
  • 01:09:25 don't need to do yarn run so it's a
  • 01:09:27 little bit different in how the package
  • 01:09:29 manager is using so since I mentioned
  • 01:09:31 npx
  • 01:09:32 I want to quickly talk about MPX so MPX
  • 01:09:35 is NPM x-cubed right so let's say you
  • 01:09:38 wanna NPM install – G create react app
  • 01:09:42 create yak tab is just an example so
  • 01:09:45 don't have worry about it in fact I'm
  • 01:09:47 gonna use react so so react was just
  • 01:09:49 another one that I use and if you if you
  • 01:09:52 have we actually install then you can do
  • 01:09:54 react new I think project this is the
  • 01:10:00 command that you get react full react
  • 01:10:03 react full so you get a binary react
  • 01:10:05 full new project and it will create a
  • 01:10:08 reacts full project for you so this is
  • 01:10:09 the command that you can use but this
  • 01:10:12 will install reacts full globally and
  • 01:10:15 then use it right so a month from now if
  • 01:10:19 I come back to the project and I do
  • 01:10:21 another react full command
  • 01:10:23 react full new project to then it will
  • 01:10:26 install then I will use the exact same
  • 01:10:28 reaction package that I installed a
  • 01:10:31 month ago if I want to get the latest
  • 01:10:34 and greatest every time I use the react
  • 01:10:37 Ville command instead of NPM install
  • 01:10:40 global I can just use npx react ville
  • 01:10:44 and then invoke the
  • 01:10:49 and px react for new project and what
  • 01:10:52 this command is going to do it will
  • 01:10:54 install a temporary copy of react ville
  • 01:10:57 so it will download the temp copy of
  • 01:11:01 react ville use it and then delete it so
  • 01:11:04 I don't have a local copy anymore every
  • 01:11:06 time I want to use the react Ville
  • 01:11:07 project I can just download it with MPX
  • 01:11:11 in one command and it will download the
  • 01:11:13 latest and greatest and then use it use
  • 01:11:16 the command that comes with it because
  • 01:11:18 the command matches the the package name
  • 01:11:20 cool so this is MPX to install something
  • 01:11:23 globally and download it but it's also
  • 01:11:27 available if you if you're in your
  • 01:11:30 project if you have something that you
  • 01:11:34 need to execute for example let's say
  • 01:11:37 that we installed – – – – dev so I'm
  • 01:11:43 gonna install – G this is another thing
  • 01:11:45 that we need to talk about and I'm gonna
  • 01:11:47 install the eslint right es lint so – D
  • 01:11:50 means development dependency it means we
  • 01:11:53 don't really need it in production this
  • 01:11:55 is just in development so if I do – D it
  • 01:11:59 will it will put it not under
  • 01:12:03 dependencies but rather under dev
  • 01:12:05 dependencies you see that so when you
  • 01:12:08 run things in production if you have
  • 01:12:10 this production flag if you have note n
  • 01:12:14 equal production and you run things with
  • 01:12:16 the production flag it will not install
  • 01:12:19 the dev dependencies right it will just
  • 01:12:22 ignore them
  • 01:12:22 it will only install the local
  • 01:12:23 dependencies because the eslint is not
  • 01:12:25 something that you want to have in
  • 01:12:26 production yes light is a good quality
  • 01:12:28 check that will check things while you
  • 01:12:30 develop developing them but here's the
  • 01:12:33 thing because just because we npm
  • 01:12:36 install the eslint doesn't mean that we
  • 01:12:38 get to execute the es link command there
  • 01:12:40 is no es linked command where is the es
  • 01:12:42 link command the es link command is deep
  • 01:12:45 under the node modules folder under dot
  • 01:12:47 bin so you can you can execute it from
  • 01:12:50 node modules been yes lint and and this
  • 01:12:53 will execute the node the es link
  • 01:12:55 command fine so it's it's not under it's
  • 01:12:58 under node modules right because NPM
  • 01:13:00 just install things under the node
  • 01:13:02 modules folder
  • 01:13:03 so if you want to execute it without
  • 01:13:05 figuring out where it is or you know how
  • 01:13:09 to execute it you can't just use the MPX
  • 01:13:11 command here as well
  • 01:13:12 and PXE eslint and this will try to find
  • 01:13:15 it under the node modules folder for you
  • 01:13:18 so MPX is a handy tool to do that
  • 01:13:20 that comes with with NPM of course if
  • 01:13:24 you have yarn you can just do the same
  • 01:13:26 thing with the arm so you're in just one
  • 01:13:27 command and it will you do that it will
  • 01:13:30 find the package for you
  • 01:13:32 now if you put things in in a in a
  • 01:13:36 script so if here we do check for
  • 01:13:39 example and we do es Linde NPM is also
  • 01:13:42 smart enough to figure out that so check
  • 01:13:46 in here to figure out where to look for
  • 01:13:49 es wins so we can just NPM check I mean
  • 01:13:52 run Jack right and it will also work as
  • 01:13:56 well so it will execute the ps1 command
  • 01:13:58 all right so this is dependencies dev
  • 01:14:01 dependencies and scripts very important
  • 01:14:05 things in package that Jason if I delete
  • 01:14:07 a dependency from package that Jason
  • 01:14:09 would it eliminate it from the project
  • 01:14:11 very good question in fact let's go
  • 01:14:13 ahead and delete lo – right so if we
  • 01:14:16 delete lo – because we not we're not
  • 01:14:20 gonna use it it does not eliminate it
  • 01:14:23 from and by default and still under node
  • 01:14:27 modules so the package that JSON file is
  • 01:14:31 just a documentation of what
  • 01:14:32 dependencies were installed but lo – is
  • 01:14:36 still under the node modules folder
  • 01:14:39 right so I will it go away if I do NPM
  • 01:14:44 install so if you do NPM install it
  • 01:14:48 reads everything that you have in
  • 01:14:50 package that JSON and it gives you all
  • 01:14:52 the missing dependencies right but it
  • 01:14:54 does not remove the you know the
  • 01:14:57 dependencies that you do not depend on
  • 01:14:59 anymore so this is just the NPM be I
  • 01:15:01 think yarn behavior is different but
  • 01:15:03 here's the thing you should not really
  • 01:15:04 do it this way if you want to kill the
  • 01:15:06 dependency you should do NPM uninstall
  • 01:15:10 lodash NPM uninstall lo – so this will
  • 01:15:13 remove it from the node modules and it
  • 01:15:16 will remove
  • 01:15:16 from packaged that Jason so my advice is
  • 01:15:20 do not touch dependencies and dev
  • 01:15:22 dependencies manually in your package to
  • 01:15:25 Jason you can only change the meta
  • 01:15:27 information you can add scripts manually
  • 01:15:29 that's fine it's just dependencies and
  • 01:15:31 dev dependencies make them go through
  • 01:15:34 the NPM command itself right this is
  • 01:15:36 just the advice unless you want to
  • 01:15:38 change the this weird character here the
  • 01:15:43 caret character which means give me
  • 01:15:45 anything that is compatible with three
  • 01:15:49 three so basically so three three would
  • 01:15:52 be fine here three four would be fine
  • 01:15:54 but not four so it says like give me
  • 01:15:56 everything that is compatible with three
  • 01:15:58 so it might give you minor version as
  • 01:16:01 well so if you want to change this and
  • 01:16:03 say you know what I want this dependency
  • 01:16:05 to be exact
  • 01:16:06 do not when you update your package
  • 01:16:08 always use the exact same dependency and
  • 01:16:10 not give me any a new version or give me
  • 01:16:14 just the patch so there are like special
  • 01:16:16 meanings for all these some things but
  • 01:16:19 this is a bit advanced these you can
  • 01:16:21 change them if you want to you can also
  • 01:16:24 just say star in here which means give
  • 01:16:26 me the latest and greatest even if it
  • 01:16:28 was es m5 I don't care just give me the
  • 01:16:30 latest so a major minor we call this you
  • 01:16:34 know that the semver string and this is
  • 01:16:37 the semper version here so major minor
  • 01:16:42 and patch so you have to learn about sin
  • 01:16:44 Verte and how how to manage those
  • 01:16:46 alright so we'll go ahead and talk about
  • 01:16:49 a HTTP next so this is how you create a
  • 01:16:52 node web server you import HTTP from
  • 01:16:56 HTTP and then you do HTTP dot create
  • 01:17:04 server so this API is from note STP duck
  • 01:17:09 crate server and it's gonna teach us a
  • 01:17:11 few things about about node so what you
  • 01:17:14 get back from here depends on the API so
  • 01:17:17 you can name it anything right in here
  • 01:17:19 you can name it anything so you don't
  • 01:17:21 have to stick with the API this this
  • 01:17:24 name here has to be create server
  • 01:17:26 because that's the function the API
  • 01:17:27 exposes but here we can name it anything
  • 01:17:29 usually I name
  • 01:17:30 this server right so this is the server
  • 01:17:32 and you make server listen on some kind
  • 01:17:39 of port so we make it listen on port
  • 01:17:41 3000 for example so this is the API to
  • 01:17:45 create a web server now if you npm start
  • 01:17:49 because that's the script that we put
  • 01:17:51 the npm start you'll notice that node
  • 01:17:54 did not exit the process did not exit
  • 01:17:58 right I did not go back to the terminal
  • 01:18:02 command and this is because of the
  • 01:18:04 listen so if I kill the listen and read
  • 01:18:06 this again it will create a server and
  • 01:18:09 then it will exit I am back to the
  • 01:18:11 terminal
  • 01:18:12 right it did not it did not continue
  • 01:18:14 running so it is important for you to
  • 01:18:17 understand that node will only continue
  • 01:18:20 running if it has a reason to continue
  • 01:18:23 to run right if it has a reason to
  • 01:18:25 continue to run and that reason is do we
  • 01:18:28 have any asynchronous calls that are
  • 01:18:31 listening do we have any handlers for
  • 01:18:34 example I can have a set timeout set
  • 01:18:37 timeout is a timer right that we can run
  • 01:18:40 say after five seconds so it gives in
  • 01:18:43 here in milliseconds and let's just do
  • 01:18:46 nothing right set timeout after five
  • 01:18:49 seconds just for just to keep node busy
  • 01:18:52 for five seconds so because there is a
  • 01:18:54 reason for node to stick around for five
  • 01:18:57 seconds when we run this code node will
  • 01:18:59 not exit for five seconds and then after
  • 01:19:03 the five seconds it will have no reason
  • 01:19:05 to stay running and it will just exit
  • 01:19:07 right so that's extremely important note
  • 01:19:10 processes will exit if they don't have a
  • 01:19:12 reason to stay running so for example if
  • 01:19:15 in here if we do server that listen
  • 01:19:19 which is a reason for node to stay
  • 01:19:21 running forever right because it's
  • 01:19:22 listening right it's listening forever
  • 01:19:24 so it's a reason for it to start just to
  • 01:19:26 stick around forever but if this listen
  • 01:19:29 function deep internally through an
  • 01:19:31 error if there's an error then that
  • 01:19:34 error is gonna make the process crash
  • 01:19:36 and exit because there's no reason for
  • 01:19:38 note to stick around there's an error so
  • 01:19:40 that process the state of that listening
  • 01:19:42 server is
  • 01:19:44 so Note will crash from exit so what
  • 01:19:46 does that mean for you in production
  • 01:19:48 that means is note by itself the note
  • 01:19:51 command by itself is not enough in
  • 01:19:53 production because if you're just
  • 01:19:55 running a single node process in
  • 01:19:56 production that process might crash and
  • 01:19:58 exit at any point so you'll have to
  • 01:20:01 monitor this process and you'll have to
  • 01:20:03 have a cluster of processes and maybe
  • 01:20:06 even a cluster of servers if you need to
  • 01:20:08 so that if one process crashes and exits
  • 01:20:11 another process is automatically started
  • 01:20:14 because it is normal for a node process
  • 01:20:17 to crash and exit and if it has no
  • 01:20:20 reason to stay around it will not stay
  • 01:20:23 around alright now this server is
  • 01:20:25 listening when we emit you will not exit
  • 01:20:28 unless there is a crash and error right
  • 01:20:31 it will not exit but it is doing nothing
  • 01:20:34 right it's just listening there is no
  • 01:20:36 handler for what to do when an HTTP
  • 01:20:39 connection comes in so this is an HTTP
  • 01:20:42 server right so there is no information
  • 01:20:45 about what to do when an HTTP connection
  • 01:20:47 comes in so right now it will probably
  • 01:20:51 say undefined if we try to access
  • 01:20:53 anything if we go to localhost localhost
  • 01:20:57 and we go to port 3000 right it will
  • 01:21:02 probably not do anything waiting for
  • 01:21:04 localhost and it will timeout because
  • 01:21:06 there is no instruction but here's the
  • 01:21:08 thing the HTTP server received the
  • 01:21:12 request it just did not handle the
  • 01:21:14 request and the way it does this is
  • 01:21:17 through the event the events concept so
  • 01:21:22 everything is handled with events so
  • 01:21:25 when when I hit that localhost port 3000
  • 01:21:27 in the browser I the the HTTP in the
  • 01:21:30 server object is going to emit an event
  • 01:21:34 that says oh I received a connection
  • 01:21:36 what should I do with it so to handle
  • 01:21:39 the connection I just handle the event I
  • 01:21:41 do something like server dot on and I
  • 01:21:45 specify the event that I'm interested in
  • 01:21:47 which is a request for Connect I don't
  • 01:21:51 know I don't know what it is I think
  • 01:21:52 it's request we'll see and in here you
  • 01:21:56 get
  • 01:21:58 what you do stuff in here so what do you
  • 01:22:01 want to do on request now if you don't
  • 01:22:03 know what the event is you can so so
  • 01:22:09 here's the thing if if if you're using a
  • 01:22:11 library like typescript for example
  • 01:22:13 which I recommend that you check out
  • 01:22:15 then you'll immediately get all the
  • 01:22:18 discoverability of everything because
  • 01:22:19 server is a typed object and we know all
  • 01:22:22 the events on it we know all the stuff
  • 01:22:25 in it but if you don't if you don't have
  • 01:22:26 this signature around then just look at
  • 01:22:29 the documentation know the API
  • 01:22:31 documentation czar really really good so
  • 01:22:34 you just go to the node API and you look
  • 01:22:36 for HTTP and you look for the on the on
  • 01:22:41 socket that on connect you look for
  • 01:22:44 connect there's no there's a lot of in
  • 01:22:47 here but I think what we're looking for
  • 01:22:49 is request right so you'll find that the
  • 01:22:52 request event here right and you can
  • 01:22:56 feel like find examples what is happen
  • 01:22:59 this is the event request right and
  • 01:23:01 here's what you get when you handle the
  • 01:23:03 event request you get a request object
  • 01:23:05 and the response object right and this
  • 01:23:08 is exactly what we're gonna do here
  • 01:23:09 we're gonna get a request object and the
  • 01:23:11 response object this is when I handle
  • 01:23:13 the request so in here you can do
  • 01:23:18 anything to new computations you can go
  • 01:23:20 ask the database for information
  • 01:23:21 whatever you need to do this function
  • 01:23:23 here which we call the callback function
  • 01:23:25 but it's really more like a handler
  • 01:23:28 function in this case is going to be
  • 01:23:30 invoked every time there is a request
  • 01:23:32 every time I put on localhost port 3000
  • 01:23:35 and hit enter that's a request right so
  • 01:23:38 let's console.log a request and let's
  • 01:23:45 response send something so hello
  • 01:23:49 HTTP so this is how you do it response
  • 01:23:52 that's the dot right not send response
  • 01:23:55 dot right hello HTTP and then response
  • 01:24:00 the end
  • 01:24:00 now don't worry about the API yet this
  • 01:24:02 this will eventually learn because these
  • 01:24:05 things the response object here is a
  • 01:24:07 stream
  • 01:24:08 we need to stream information this is
  • 01:24:10 why we needed to do that in for the HTTP
  • 01:24:12 protocol to say oh I'm done but I want
  • 01:24:15 to show you that response that right now
  • 01:24:17 we need to restart this process every
  • 01:24:20 time you change something in a node
  • 01:24:22 project you have to restart node know
  • 01:24:24 does not have the development mode
  • 01:24:26 out-of-the-box
  • 01:24:27 so you have to restart it and now if I
  • 01:24:29 go to localhost port 3000 I will see
  • 01:24:33 hello HTTP so the message that I wrote
  • 01:24:36 Suresh túcume and note how I did all of
  • 01:24:39 this without any libraries I'm not using
  • 01:24:43 Express I installed Express but I'm not
  • 01:24:45 using it I'm just using the built-in
  • 01:24:47 HTTP library now if you look at the
  • 01:24:50 terminal for the thing that we just did
  • 01:24:52 you'll notice that we have two requests
  • 01:24:55 not just one request although i refresh
  • 01:24:57 the browser just once I ask for that
  • 01:25:00 page just once so for some reason this
  • 01:25:03 handler is being called twice
  • 01:25:06 every time we request so why well you
  • 01:25:11 need to discover why so to discover why
  • 01:25:14 maybe you can print information about
  • 01:25:16 the request why am I doing this request
  • 01:25:18 and I know that there is a request that
  • 01:25:20 URL that you can use to figure out what
  • 01:25:23 URL is being requested so now if we go
  • 01:25:26 back and write and we have to restart
  • 01:25:28 the server remember we have to restart
  • 01:25:29 the server if we go back and refresh the
  • 01:25:32 browser once and go back to see what
  • 01:25:36 requests are being made here Oh check it
  • 01:25:39 out so we're requesting that and we're
  • 01:25:41 requesting the fav icon so the browser
  • 01:25:44 is requesting the Fath icon by default
  • 01:25:46 that's chrome just trying to find the
  • 01:25:47 fab I come for that website so you have
  • 01:25:50 to be aware of these things right that
  • 01:25:52 you invoke the the request every time
  • 01:25:57 there is a request from the browser and
  • 01:26:01 the browser might do requests internally
  • 01:26:04 for its own rendering write an apple an
  • 01:26:08 iPhone might request the you know I'd
  • 01:26:10 the Apple compressed icon for the tablet
  • 01:26:14 and Apple compressed icon for the iPhone
  • 01:26:17 right it will request the four things
  • 01:26:19 actually so
  • 01:26:21 you have to handle these right now how
  • 01:26:25 do you handle right how do you say Oh
  • 01:26:28 what should we do if the request is I
  • 01:26:30 have I can request we should serve a
  • 01:26:33 small image so you would need to come in
  • 01:26:37 here and because of how limited this
  • 01:26:40 library is you would need to come in
  • 01:26:42 here and say something you know what if
  • 01:26:43 the request that URL is to have icon
  • 01:26:46 then do something different right you
  • 01:26:50 would have to do something like this to
  • 01:26:52 handle routes basically right maybe in
  • 01:26:56 here you just want to respond response
  • 01:26:59 not right nothing no I don't have a 5
  • 01:27:01 icon else else right hello HTTP right so
  • 01:27:06 now the 5 icon is not gonna get any
  • 01:27:08 response where previously it got a
  • 01:27:11 string response right so you'll have to
  • 01:27:12 you have to have extra logic to deal
  • 01:27:15 with routing basically because this is a
  • 01:27:18 very raw HTTP package that has no no you
  • 01:27:24 know rich API for dealing with routes or
  • 01:27:26 dealing with files what if you want to
  • 01:27:28 send the file to the browser right what
  • 01:27:31 if I want to go to like you know give me
  • 01:27:33 slash a dot PNG right if I want to get a
  • 01:27:37 file an image I don't have a way to do
  • 01:27:41 that I have to use a different library
  • 01:27:43 to read the image from the operating
  • 01:27:46 system and then write the binary
  • 01:27:50 information that I get by reading the
  • 01:27:52 image from the operating system into the
  • 01:27:55 response stream in here so you have
  • 01:27:57 limited capabilities but everything is
  • 01:28:01 possible you can still do that you can
  • 01:28:03 still read from the file system and and
  • 01:28:06 and use the the the node response stream
  • 01:28:10 to write to it but there is a better way
  • 01:28:14 for dealing with this which is to
  • 01:28:15 introduce a library specialized in that
  • 01:28:17 kind of stuff all the basic needs for
  • 01:28:20 you when you do a web server and that
  • 01:28:22 library the most popular library is
  • 01:28:25 Express so Express GS is well it's
  • 01:28:29 really a framework around web servers
  • 01:28:32 and every
  • 01:28:35 all expect no developers to be familiar
  • 01:28:37 with at least one web framework and
  • 01:28:40 Express is just the popular one out
  • 01:28:43 there so how to use Express that's what
  • 01:28:45 we're gonna do next but before we do
  • 01:28:47 that before we do that you might see
  • 01:28:50 this handler written in a different way
  • 01:28:54 instead of server the on request you
  • 01:28:57 might see it in here directly as a
  • 01:29:00 parameter to create server so this is
  • 01:29:03 just a different syntax that you can use
  • 01:29:06 without dealing with the dot on request
  • 01:29:09 kind of thing so so actually hides the
  • 01:29:13 dot on request
  • 01:29:15 thing but I don't like this syntax
  • 01:29:17 because I I need you to learn that this
  • 01:29:20 is an event right and just like requests
  • 01:29:23 there are other events there's connect
  • 01:29:25 and disconnect and and many other events
  • 01:29:27 and node packages are really burst into
  • 01:29:32 this event nature so everything is an
  • 01:29:35 event emitter we call server here an
  • 01:29:37 event emitter so and you probably need
  • 01:29:41 to learn your own event emitting skills
  • 01:29:45 you have to be able to create an event
  • 01:29:47 emitter and listen to and make it emit
  • 01:29:50 events and listen to events because this
  • 01:29:52 this this observer model in general we
  • 01:29:57 call this the observer model you observe
  • 01:29:58 something on something else and it
  • 01:30:01 allows you to abstract your libraries so
  • 01:30:04 that they don't depend on each other
  • 01:30:05 they just send messages right and you
  • 01:30:08 listen to messages and you act on four
  • 01:30:11 messages
  • 01:30:11 so this react fullness for the in the
  • 01:30:15 observer model is really handy and this
  • 01:30:17 is why it's all over the place in in in
  • 01:30:19 note by default note has a limited
  • 01:30:24 mechanism to to handle asynchronous
  • 01:30:28 calls which is the callbacks and we
  • 01:30:31 should probably talk about it a little
  • 01:30:32 bit after after in the third hour I'm
  • 01:30:34 going to talk a little bit about
  • 01:30:35 callbacks and promises in general
  • 01:30:37 because this is this is shifting from
  • 01:30:39 node into modern JavaScript but in the
  • 01:30:43 next five or ten minutes let's explore
  • 01:30:46 Express so here's how you
  • 01:30:49 a server would Express instead of
  • 01:30:51 importing from HTTP we don't need an
  • 01:30:52 HTTP you import Express from Express now
  • 01:30:58 what do you get here when you import
  • 01:31:00 Express you get a function so if you go
  • 01:31:03 to the source code of Express you'll see
  • 01:31:05 that the top-level import is a function
  • 01:31:08 just like the function that we used when
  • 01:31:10 we were testing you know the multiple
  • 01:31:11 calls so if you want to use Express and
  • 01:31:15 I'm just gonna comment out everything
  • 01:31:16 here just to keep things just for
  • 01:31:19 Express if you want to use Express you
  • 01:31:20 because it's a function you just call it
  • 01:31:22 and by calling a function what you get
  • 01:31:25 back is a server instance so basically
  • 01:31:28 just like you know the create server
  • 01:31:31 function this Express function top-level
  • 01:31:33 is equivalent to the create server
  • 01:31:35 function all right so the listen is
  • 01:31:38 actually exactly the same you just
  • 01:31:39 listen on a port so 3000 just like that
  • 01:31:42 you listen on a port 3000 just like you
  • 01:31:45 did for the HTTP API but the route
  • 01:31:48 handling is a little bit different right
  • 01:31:51 the route handling is a little bit
  • 01:31:54 different so instead of doing server dot
  • 01:31:56 on you would do server got get and this
  • 01:32:02 takes in an actual route get slash and
  • 01:32:06 this will give you access to request and
  • 01:32:09 response right so similar to dot on now
  • 01:32:13 it's but it's now get so and you get
  • 01:32:16 access to request and response just like
  • 01:32:19 that but you also the response is a
  • 01:32:21 wrapper around the raw stream response
  • 01:32:24 that you get from node so in here you
  • 01:32:27 can do other things you can do send or
  • 01:32:30 send file if you want to this does not
  • 01:32:32 exist in you know the regular actually
  • 01:32:38 to be library so in here I can even send
  • 01:32:40 an object if I want to send an object so
  • 01:32:44 message is hello HTTP so you see how
  • 01:32:48 hello express right because now we're in
  • 01:32:51 Express so you have some and you don't
  • 01:32:53 have to do response that end send is
  • 01:32:55 gonna deal with this so it will abstract
  • 01:32:58 some of these complexities in here and
  • 01:33:00 also you get to be
  • 01:33:02 very clean in the way you define your
  • 01:33:04 routes because now I can also do fav
  • 01:33:08 icon have icon ICO in fact I can do fab
  • 01:33:14 icon star so if you call five I conduct
  • 01:33:17 PNG I'm gonna respond to this route
  • 01:33:19 there's like globbing going on here in
  • 01:33:22 the routes so this and in here you would
  • 01:33:25 send file you know the file icon
  • 01:33:28 whatever the path does have icon so you
  • 01:33:30 send the file directly in here right and
  • 01:33:33 and express is gonna deal with reading
  • 01:33:36 the file from the file system and
  • 01:33:38 streaming it to the requester see how
  • 01:33:42 this rich API is it's really still basic
  • 01:33:47 but it gives you some capabilities to
  • 01:33:50 just do features out of the box instead
  • 01:33:52 of having to deal with this complexity
  • 01:33:54 here but it is a wrapper around the raw
  • 01:33:58 HTTP in fact what you get here this
  • 01:34:01 listen method is exactly it's just
  • 01:34:03 forwarded to the HTTP listen method
  • 01:34:05 because express itself is just a wrapper
  • 01:34:07 around the built in HTTP framework that
  • 01:34:12 you get with note now we didn't really
  • 01:34:16 talk about the difference between import
  • 01:34:17 and export and require there is a slight
  • 01:34:20 difference and and that you need to be
  • 01:34:23 aware of which is import is statically
  • 01:34:28 evaluated while required is dynamically
  • 01:34:31 evaluated so what does that mean that
  • 01:34:34 means import is always top level you
  • 01:34:38 can't for example import something
  • 01:34:40 inside another function you can't do
  • 01:34:43 that you can't import something inside
  • 01:34:44 the function that is invalid JavaScript
  • 01:34:47 syntax why because import is statically
  • 01:34:50 evaluated while you can totally require
  • 01:34:54 something inside a function when if it's
  • 01:34:58 note is why I require you can require
  • 01:35:00 something inside a function because
  • 01:35:02 require is dynamically evaluated and
  • 01:35:04 then cached so that's a big difference
  • 01:35:07 between import and export between import
  • 01:35:10 and require now what if you want to
  • 01:35:12 dynamically import something with echo
  • 01:35:16 modules not with note if you make the
  • 01:35:19 decision to use a coma script modules
  • 01:35:21 and you want to lazily import something
  • 01:35:23 basically you still have the option
  • 01:35:25 because there is an import function as
  • 01:35:27 well so ACMA script has an import
  • 01:35:29 function which behaves kind of like
  • 01:35:31 require in in in how it you can
  • 01:35:35 dynamically call it and it gives you
  • 01:35:37 back a promise so what this gives you
  • 01:35:40 back is a promise now this is how import
  • 01:35:43 is different than required which is
  • 01:35:45 required although dynamically evaluated
  • 01:35:48 it is synchronous so require is
  • 01:35:50 synchronous while import is asynchronous
  • 01:35:54 so we're gonna talk another five minute
  • 01:35:56 break and come back top of the hour and
  • 01:35:58 talk about synchronous and asynchronous
  • 01:35:59 talk about callbacks and promises and
  • 01:36:03 I'm well I'll go over some lore of lore
  • 01:36:06 of the modern features in JavaScript
  • 01:36:09 that you can use out of the box in nodes
  • 01:36:11 and it will make your code for node
  • 01:36:12 better and then we'll leave some some
  • 01:36:14 some time for questions because I'm
  • 01:36:16 pretty sure you're gonna have some all
  • 01:36:18 right so no requires not really better
  • 01:36:20 than import but it is just different
  • 01:36:25 capabilities so if you want to have
  • 01:36:28 something synchronously imported then
  • 01:36:31 you can have a wrapper around this
  • 01:36:33 import something it's just require
  • 01:36:35 existed before Xmas script decided on
  • 01:36:39 this import export syntax and its future
  • 01:36:42 is probably that it's gonna exist in
  • 01:36:46 node but it's not the way to go
  • 01:36:49 but not for a few years we still need a
  • 01:36:52 few years to stabilize this
  • 01:36:53 import/export but I would say starting
  • 01:36:56 node 14 no one is gonna remember I
  • 01:36:58 require everyone is just gonna use
  • 01:37:00 import and export and the reason is this
  • 01:37:03 is going to work natively in the browser
  • 01:37:05 so you can take the code as is and it
  • 01:37:08 will work in the browser and the browser
  • 01:37:10 is gonna do this the smartest thing to
  • 01:37:12 try to fetch things in parallel from
  • 01:37:16 locations so you would have to import
  • 01:37:20 from an edge to actually be URL of
  • 01:37:21 course you would have to import from
  • 01:37:23 like github.com slash Express and and
  • 01:37:26 things will work just like that and you
  • 01:37:29 me too bundle things because right now
  • 01:37:31 when we ship things to a browser we need
  • 01:37:35 to bundle them we need to bundle them
  • 01:37:37 into one file so that the browser can
  • 01:37:39 understand them but in the very near
  • 01:37:41 future and by near-future I mean it is
  • 01:37:44 possible already but it's still
  • 01:37:47 experimental but it's probably next year
  • 01:37:49 or the year after that we will just ship
  • 01:37:53 the same code to the browser and the
  • 01:37:55 browser will work all right so what is
  • 01:37:58 bundling really quick bundling is when
  • 01:38:00 you take a front-end pack a front-end
  • 01:38:02 application that exists over many files
  • 01:38:06 and and you pack them into a single file
  • 01:38:09 we call this bundling and then you ship
  • 01:38:11 a single file to the browser so before
  • 01:38:15 we talk about callbacks and promises a
  • 01:38:20 little bit more about events let me let
  • 01:38:24 me tell you about so if you have if if
  • 01:38:27 you if you wanna when you develop your
  • 01:38:30 node applications you're gonna be
  • 01:38:31 editing files often right and having to
  • 01:38:37 restart things is gonna be is gonna get
  • 01:38:39 annoying really fast because it's a
  • 01:38:42 running server right if you have a
  • 01:38:43 running server so let's keep this
  • 01:38:45 running a way to keep the server or any
  • 01:38:50 file running is to do process the STD
  • 01:38:54 end resume now this is advanced but
  • 01:38:58 don't worry about it so if I have a
  • 01:38:59 server that's running and I want to
  • 01:39:02 change things having to restart to the
  • 01:39:05 server is annoying so what you can do is
  • 01:39:07 you can use tools to watch the files and
  • 01:39:11 trigger or restart on save and one of
  • 01:39:14 them is node 1 so we get NPM install –
  • 01:39:19 dev node 1 node mom for nude monitor and
  • 01:39:23 all you need to do is just switch your
  • 01:39:25 node command to use node mod instead of
  • 01:39:29 nodes so instead of node here
  • 01:39:31 node mod and then it will auto restart
  • 01:39:35 things for you when you save the files
  • 01:39:38 so now that we're running with node mod
  • 01:39:39 if I change the string and just
  • 01:39:43 save it will automatically restart note
  • 01:39:45 for me so this is handy in development
  • 01:39:48 so because otherwise it's gonna be
  • 01:39:50 annoying you can hate it of course there
  • 01:39:52 are a few other tools you can use p.m.
  • 01:39:54 to itself you there's a forever NPM
  • 01:39:57 package though there's plenty of
  • 01:40:00 packages to do that the thing and this
  • 01:40:02 is general in terms of note in general
  • 01:40:06 when you need you need a single feature
  • 01:40:07 you have a lot of options to pick from
  • 01:40:10 for example a web framework like Express
  • 01:40:14 there are hundreds of them not not tens
  • 01:40:17 hundreds of them and and and every one
  • 01:40:21 of them has different advantages and
  • 01:40:23 disadvantages so you're gonna you're
  • 01:40:26 gonna have to make decisions on what
  • 01:40:27 libraries to use and I would and there
  • 01:40:32 are some guidelines on how to pick a
  • 01:40:34 library but I would go for the popular
  • 01:40:36 library that is maintained and tested so
  • 01:40:39 try to see if libraries maintain the
  • 01:40:41 tested and oh and it's responsive or
  • 01:40:45 it's closing issues on github its
  • 01:40:47 responding to issues on github and also
  • 01:40:49 take a look at the code so don't be
  • 01:40:51 afraid of the code and take a look at
  • 01:40:53 how the library is written and are they
  • 01:40:56 using clean code practices are they
  • 01:40:58 using good things like typescript for
  • 01:41:00 example that's a good sign if the
  • 01:41:02 library is using typescript in its code
  • 01:41:03 base that means they care about you know
  • 01:41:05 tithes they care about their API all
  • 01:41:09 right so this is node mom now let's talk
  • 01:41:15 about callbacks and promises so I'm just
  • 01:41:17 gonna kill this restart so it's a clean
  • 01:41:20 exit nothing to do and what I'm going to
  • 01:41:22 talk about is the callback pattern that
  • 01:41:25 you're gonna see so if you have a
  • 01:41:28 function that is wired for the callback
  • 01:41:34 function let's let's use the execute
  • 01:41:37 command so if you want to execute a
  • 01:41:39 command from within your node process
  • 01:41:42 you can use child processes what we call
  • 01:41:46 the child process library child process
  • 01:41:49 so this is how you use the child process
  • 01:41:51 you just require I mean import import
  • 01:41:57 something from the child process and I'm
  • 01:41:59 going to talk about this syntax in just
  • 01:42:00 a little bit so you import something
  • 01:42:02 from child processing you use it now you
  • 01:42:05 have a few things that you can import
  • 01:42:06 from child process I'm gonna import the
  • 01:42:08 exact method the exact methods from
  • 01:42:11 child process so this exact method takes
  • 01:42:14 two arguments or three I don't I'm not
  • 01:42:17 sure we'll check the API if we need to
  • 01:42:19 the first argument is the command to be
  • 01:42:23 executed
  • 01:42:24 so let's LS / right this is the command
  • 01:42:29 to be executed so I'm gonna execute LS /
  • 01:42:31 from within node from within node and it
  • 01:42:36 gives you access to the output through a
  • 01:42:42 callback so this is the callback that it
  • 01:42:45 will invoke so in here you get a
  • 01:42:47 function so the callback is just a
  • 01:42:48 function I'm using arrow functions here
  • 01:42:50 but you can use a regular one and I
  • 01:42:52 think it gives access to out and error
  • 01:42:56 I'm not sure maybe it gives X so I have
  • 01:43:00 no idea what the API is but the
  • 01:43:04 documentation to the rescue so let's go
  • 01:43:06 look up child processes child processes
  • 01:43:12 and go for exec what is the API for
  • 01:43:15 exact command options callback so you
  • 01:43:19 need to get comfortable reading by the
  • 01:43:20 way this this weird syntax that they use
  • 01:43:23 for documentation so this weird syntax
  • 01:43:25 means this exact command takes the
  • 01:43:28 command which is a string and then it
  • 01:43:30 takes an optional options so these
  • 01:43:32 square brackets around options mean
  • 01:43:35 they're optional and also it takes an
  • 01:43:37 optional callback and the options is an
  • 01:43:40 object so you can skip it the callback
  • 01:43:41 is a function and these are the three
  • 01:43:43 arguments that you get their STD Adam
  • 01:43:45 st-pierre I would name them exactly the
  • 01:43:47 same so error STD out STD air these are
  • 01:43:51 the things that you get now I'm not
  • 01:43:54 focusing about the API itself what I
  • 01:43:56 want to focus on is the callback
  • 01:43:58 mechanism in here so if I'm interested
  • 01:44:00 in the output I just do STD out right so
  • 01:44:04 if I execute this file with NPM start
  • 01:44:09 because it's why are we
  • 01:44:11 some and everything then we will get a
  • 01:44:14 module not found child
  • 01:44:17 process as a child processes child
  • 01:44:22 process
  • 01:44:23 oh I misspelled it so its child process
  • 01:44:27 and check it out
  • 01:44:28 boom I see the LS / the output of that
  • 01:44:33 last slash you see that so x AK executed
  • 01:44:37 LS slash and then it gave me access to
  • 01:44:40 the SD be out now if the command have
  • 01:44:43 STD error it will give me access to us
  • 01:44:46 TV error instead but why do we have two
  • 01:44:49 errors there's an error here and then
  • 01:44:51 there is an STD in here and the reason
  • 01:44:53 is this error is related to the callback
  • 01:44:56 itself not to the command right so it
  • 01:45:01 depends on how you deal with it but the
  • 01:45:03 callback pattern in general is that
  • 01:45:05 there's always an error first argument
  • 01:45:08 this is the callback pattern this is
  • 01:45:10 what happens if we have an error in
  • 01:45:13 executing the command not an actual
  • 01:45:15 error from STD error itself not an
  • 01:45:18 because every command might also result
  • 01:45:20 in an error right every every string
  • 01:45:23 that you pass in here might result in an
  • 01:45:24 error in that case you get it an STD air
  • 01:45:26 but also exec itself
  • 01:45:28 might might have an error maybe it's
  • 01:45:32 executing a command over as SSH and
  • 01:45:35 network is slow so there's plenty of
  • 01:45:38 ways for a command to fail so you always
  • 01:45:40 get this error first argument and you
  • 01:45:43 have to handle it because if you don't
  • 01:45:45 handle it
  • 01:45:46 the error is uh swallowed them it's not
  • 01:45:49 cached
  • 01:45:50 it's not cached and that means you have
  • 01:45:52 a bug so because the command is gonna
  • 01:45:54 continue to run STD out will probably be
  • 01:45:57 undefined and because he did not handle
  • 01:46:00 the error so you always have to handle
  • 01:46:02 the air what do you want to do if there
  • 01:46:04 is an error right what do you want to do
  • 01:46:07 if there's an error and depends on how
  • 01:46:10 this module is used you have to do
  • 01:46:14 something you can for example throw
  • 01:46:16 there this is the safest thing you can
  • 01:46:18 do is throw the error again because if
  • 01:46:20 you don't it's not again it's not cached
  • 01:46:23 at all and it will
  • 01:46:24 just go silent but throwing an error
  • 01:46:27 will make that process will make the
  • 01:46:29 error poeple up to the consumer of this
  • 01:46:32 of this module you can also say you know
  • 01:46:35 what if there's an error here something
  • 01:46:38 serious
  • 01:46:38 it happened and I don't know and I
  • 01:46:40 didn't want to risk it I'm gonna process
  • 01:46:42 that exit
  • 01:46:43 I'm gonna make the process crash on exit
  • 01:46:44 because I don't know what happened so
  • 01:46:47 I'm gonna make it crash and exit with a
  • 01:46:49 status code of 1 which means this is not
  • 01:46:51 a clean exit we don't know what happened
  • 01:46:53 and error happened and let's just crash
  • 01:46:55 and exit because you don't want to have
  • 01:46:57 a process running in an unknown state
  • 01:46:59 and when you get an error you kind of
  • 01:47:01 have an unknown state because you don't
  • 01:47:03 know what there is if you want to handle
  • 01:47:06 some kind of error you can you can have
  • 01:47:08 an if statement here it says oh if the
  • 01:47:10 error is kind of this kind you know one
  • 01:47:13 kind or another kind if the error is
  • 01:47:15 file not found or command not found I'm
  • 01:47:18 gonna deal with it right but otherwise
  • 01:47:21 process and exit this is how you handle
  • 01:47:23 error you should crash the process and
  • 01:47:25 exit and then the monitor is gonna start
  • 01:47:28 a new process that does not have this
  • 01:47:30 error state all right so this is the
  • 01:47:32 error first the error first callback
  • 01:47:36 signature is that the callback itself is
  • 01:47:40 the last argument to the host function
  • 01:47:43 so we call this the host function right
  • 01:47:45 it might have a series of commands and
  • 01:47:47 then the callback is a function that is
  • 01:47:49 the last parameter for that the function
  • 01:47:54 is gonna receive it so it's the the last
  • 01:47:56 parameter for that function and then the
  • 01:47:59 callback itself has three parameters
  • 01:48:01 error STD out and STD I mean at least at
  • 01:48:06 least one parameter which is error and
  • 01:48:08 then you get the data through the second
  • 01:48:10 and the third and the fourth parameter
  • 01:48:12 if if they exist right so it's we call
  • 01:48:15 this pattern the error first callback
  • 01:48:18 pattern and this is what existed in node
  • 01:48:20 since day one and it's still and it's
  • 01:48:23 still supported today but there is a
  • 01:48:26 different pattern today which is to use
  • 01:48:28 promises instead of this callback
  • 01:48:30 mechanism
  • 01:48:31 so if exec was a promised based function
  • 01:48:36 then this is how you would use it you
  • 01:48:38 would say exact and you give it the
  • 01:48:40 command LS right and this gives you back
  • 01:48:45 the SDV act just like that just as if it
  • 01:48:49 was not asynchronous the reason we
  • 01:48:52 needed callback here is because exact is
  • 01:48:54 asynchronous it needs to go to the
  • 01:48:57 operating system instruct the operating
  • 01:49:00 system to do something and then take
  • 01:49:02 results from when the operating system
  • 01:49:04 is done which might be a few seconds
  • 01:49:06 later and then do something with the
  • 01:49:09 results so this is an asynchronous
  • 01:49:10 function so it's not synchronous I
  • 01:49:13 cannot just I cannot I cannot do that I
  • 01:49:16 cannot call it synchronously I have to
  • 01:49:19 call it asynchronously
  • 01:49:20 but if exact is promise based then I can
  • 01:49:24 await or first let's do that then
  • 01:49:27 I can do dot then yeah let's just do a
  • 01:49:31 wait so I'm not gonna like we don't have
  • 01:49:33 time to talk about that then and you
  • 01:49:34 should really just use awake anyway so
  • 01:49:36 you just await on exact but to await on
  • 01:49:40 a function you have to wrap it in an
  • 01:49:44 async function so you would do you would
  • 01:49:47 like define a main function for example
  • 01:49:49 and you label it as async right and then
  • 01:49:54 you call the main function because this
  • 01:49:56 is a thing so you cannot just await on
  • 01:49:58 something you have to await on something
  • 01:50:00 inside an async function but this is a
  • 01:50:04 cleaner than the other part here the
  • 01:50:06 other callback mechanism here because
  • 01:50:09 callback has many problems and the
  • 01:50:12 probably the most famous problem is how
  • 01:50:16 when you start making operations depend
  • 01:50:20 on each other you would have to start
  • 01:50:22 nest callbacks so you would nest
  • 01:50:24 callback to in callback one you would
  • 01:50:26 nest callback three in callback – and we
  • 01:50:29 call this the Pyramid of doom
  • 01:50:31 you know the callback help problem but
  • 01:50:33 that's not really the most important
  • 01:50:36 problem at that callbacks the most
  • 01:50:37 important problem about callbacks
  • 01:50:39 is that you lose control when you have
  • 01:50:43 when you do things in a callback because
  • 01:50:45 unless you have trust in the system
  • 01:50:47 unless you have trust that exact is
  • 01:50:49 gonna actually invoke the call
  • 01:50:51 one time right then you don't have
  • 01:50:55 control you don't know exact might call
  • 01:50:57 your callback five times I don't know
  • 01:50:59 you don't have control over that while
  • 01:51:02 here you have a little bit more control
  • 01:51:04 you have a little bit more trust that
  • 01:51:06 this exact is a promise and because it's
  • 01:51:09 a promise it's gonna either resolve or
  • 01:51:11 reject and it's gonna resolve or reject
  • 01:51:13 only once so by moving it into a promise
  • 01:51:17 based API you have a little bit more
  • 01:51:19 trust into how this API is gonna behave
  • 01:51:23 and this is especially important if
  • 01:51:25 you're using something that's not
  • 01:51:27 built-in not exact if you're using
  • 01:51:29 something from Express to not built-in
  • 01:51:31 so you're really relying on the
  • 01:51:33 maintainer zuv Express package the
  • 01:51:36 people who are wrote the code for the
  • 01:51:37 Express package you're depending on
  • 01:51:39 their coding skills right and you never
  • 01:51:43 know but if they give you the interface
  • 01:51:46 as a promise then you get back a little
  • 01:51:48 bit more trust because you know the
  • 01:51:51 promise is an echo script you know a
  • 01:51:53 built in JavaScript thing that is going
  • 01:51:55 to either resolve or reject and it will
  • 01:51:57 do that either of these once so you have
  • 01:51:59 a little bit more trust and also you
  • 01:52:01 have a little bit more control into the
  • 01:52:03 flow of the program you get STD out and
  • 01:52:06 you can do something after a speedy out
  • 01:52:08 and if you want to nest dependencies you
  • 01:52:11 can just do them in line you don't have
  • 01:52:13 to nest callback functions you happen
  • 01:52:15 you don't have to do anything so it's
  • 01:52:16 much cleaner and better way to just
  • 01:52:21 handle asynchronous in general so here's
  • 01:52:24 my advice to me unless you have to deal
  • 01:52:26 with something that only supports
  • 01:52:29 callbacks and nothing else
  • 01:52:31 do not use callbacks always use promises
  • 01:52:34 now here's the thing exact is not a
  • 01:52:36 promise based function so this will not
  • 01:52:39 work if we try to console the log STD
  • 01:52:43 out it will not work because because
  • 01:52:48 actually no it did not work it gives me
  • 01:52:51 an error here a big error and I did not
  • 01:52:53 handle the error right you remember that
  • 01:52:55 I did not handle the error so in here I
  • 01:52:58 got some kind of object child process
  • 01:53:02 it's not really the output them
  • 01:53:04 interested in
  • 01:53:05 so how do you make it how do you promise
  • 01:53:09 if I this how do you make it into a
  • 01:53:11 promise well luckily note has a tool
  • 01:53:15 it's called
  • 01:53:18 you tell you tells you tells liking so
  • 01:53:23 if this comes built-in and one of the
  • 01:53:26 utilities the utility functions is is
  • 01:53:32 promise if I so you can call you tells
  • 01:53:35 the promise of I and you pass an exact
  • 01:53:41 and it gives you back a promise fight
  • 01:53:43 version of exact so we can make it like
  • 01:53:47 exactly the promise version in here
  • 01:53:49 right and in here since we have a
  • 01:53:52 promise of I'd version of exact now we
  • 01:53:53 can use the promise if I'd version of
  • 01:53:56 exact and things will work fine unless
  • 01:54:00 you tells is it you tell yeah there you
  • 01:54:05 go so we got STD out it looks like it
  • 01:54:08 returns back an object so it's util not
  • 01:54:10 utils it returns back an object right
  • 01:54:13 and this object has STD out and it has
  • 01:54:16 STV error right so if you have an object
  • 01:54:19 like this and you want to extract the
  • 01:54:22 STD out all you need to do is destructor
  • 01:54:24 the STD out and now we're back into the
  • 01:54:27 same the same thing that's be beyond
  • 01:54:29 cool
  • 01:54:31 now what about STD air and air so STD
  • 01:54:35 air is just another one so if you want
  • 01:54:36 STD error you get it here STD error if
  • 01:54:39 you want error itself that's a special
  • 01:54:42 case because now we're using a promise
  • 01:54:45 that means we have to we have to use the
  • 01:54:47 promise API to work with error so we can
  • 01:54:50 we can do that we can do dot catch in
  • 01:54:53 here because this is a promise and this
  • 01:54:56 will give you access to the air or you
  • 01:54:58 can do the following you can use
  • 01:55:00 javascript native error caching
  • 01:55:02 mechanism you put everything in a
  • 01:55:04 try-catch statement so you put your code
  • 01:55:07 in a try-catch statement and this will
  • 01:55:09 give you access to the error that
  • 01:55:11 happens when you execute stuff so in
  • 01:55:13 here you would do something with there
  • 01:55:15 and here you will process the exit if if
  • 01:55:17 you catch something that you
  • 01:55:19 know that you're you know you're
  • 01:55:20 unfamiliar with or or just throw it
  • 01:55:23 again report it and throw it and ignore
  • 01:55:25 it whatever you want to do with the
  • 01:55:27 error right so this API is better on so
  • 01:55:31 many levels and and again the the the
  • 01:55:36 two main important levels here are trust
  • 01:55:39 and control but also imagine that you
  • 01:55:41 want to do asynchronous calls in in a
  • 01:55:44 loop like if you want to do a loop and
  • 01:55:47 every iteration of the loop you have to
  • 01:55:49 do an asynchronous call if you're doing
  • 01:55:51 it with callbacks it gets ugly because
  • 01:55:54 you have to define the boundaries of
  • 01:55:58 your loop manually you have to keep it
  • 01:56:00 counter and and and check the counter to
  • 01:56:03 do things in the loop but if you want to
  • 01:56:05 do the asynchronous operation that is
  • 01:56:11 promise based in a loop all you have to
  • 01:56:13 do is just stick it inside the loop and
  • 01:56:15 things will just work so there are many
  • 01:56:17 benefits now some api's
  • 01:56:20 are promised by default right so every
  • 01:56:25 light every decent library out there now
  • 01:56:27 supports promises right including node
  • 01:56:30 itself node itself is going to support
  • 01:56:32 promises so you would do something like
  • 01:56:35 slash promises for example and it will
  • 01:56:38 give you a version that support promises
  • 01:56:40 out-of-the-box now they're working on
  • 01:56:42 that the the FS the FS module for
  • 01:56:45 example have a promise based API so if
  • 01:56:48 you look up the FS module which is
  • 01:56:50 something that you need to explore which
  • 01:56:53 is have to work with the file system in
  • 01:56:56 node so if you look up FS in here
  • 01:56:58 fastest them how to read files and write
  • 01:57:01 to files and create copy files and
  • 01:57:03 rename files and delete directories this
  • 01:57:05 is the biggest module in node right and
  • 01:57:07 it is the most used module because we
  • 01:57:11 work with files a lot right but if you
  • 01:57:13 look at it you'll see that it has a
  • 01:57:15 promises API so you don't even need to
  • 01:57:18 promise if I things you get promises out
  • 01:57:20 of the box and and many other libraries
  • 01:57:26 support both callbacks and promises they
  • 01:57:29 support callbacks for backwards
  • 01:57:31 compatibility but they also
  • 01:57:33 promises because that's that's the
  • 01:57:34 direction where we're going with
  • 01:57:36 asynchronous with asynchronous calls now
  • 01:57:39 if you notice I didn't really use the
  • 01:57:41 promised API I didn't do dot Ven or duck
  • 01:57:43 catch I just used async await right
  • 01:57:45 because it's just so much better
  • 01:57:47 dot then so the promise it sells gives
  • 01:57:50 you back the trust but async away gives
  • 01:57:53 you much better flow control so you flow
  • 01:57:56 your your your code in in a better way
  • 01:57:59 you control what happens with your code
  • 01:58:02 so you and you don't need to nest
  • 01:58:04 functions right so there's much benefits
  • 01:58:06 to async await so that is it about
  • 01:58:11 callbacks and promises so if you have
  • 01:58:15 any questions about that shoot up now
  • 01:58:17 callback some promises both of them
  • 01:58:19 really are limited in terms of handling
  • 01:58:24 handling multiple events right handling
  • 01:58:28 multiple events so there are there this
  • 01:58:30 is appropriate here because we have a
  • 01:58:33 single event this asynchronous nature
  • 01:58:35 here is a single event that we need to
  • 01:58:37 execute a single command and get back a
  • 01:58:39 single output right but what if we have
  • 01:58:42 like a monitor of commands what if we
  • 01:58:46 are monitoring the memory for example
  • 01:58:48 right we're monitoring the memory this
  • 01:58:50 application is using we would need to
  • 01:58:53 pull many commands on an interval we
  • 01:58:56 need we need many events right or we're
  • 01:59:00 monitoring the memory and we're
  • 01:59:01 interested in knowing what's gonna
  • 01:59:03 happen when it reaches certain
  • 01:59:06 thresholds like after 100 me 200 me
  • 01:59:08 right so if you have an asynchronous
  • 01:59:11 operation that has multiple events this
  • 01:59:15 is where you need to use an event
  • 01:59:17 emitter you need to use an event emitter
  • 01:59:19 and using an event emitter is
  • 01:59:22 straightforward because it comes it's
  • 01:59:24 supported in node itself so this is this
  • 01:59:26 is something that you need to get
  • 01:59:28 familiar with you import event emitter
  • 01:59:30 from events this is also built-in you
  • 01:59:34 import event in metal from events and
  • 01:59:36 you create an event emitter an event a
  • 01:59:41 matter object using this syntax new
  • 01:59:44 event
  • 01:59:45 emitter and now you can if it emit
  • 01:59:50 events you can say okay FET the matter
  • 01:59:52 I'm going to emit this event just let's
  • 01:59:55 call it change something has changed you
  • 02:00:00 can you can omit any event right you can
  • 02:00:02 omit the same event multiple times right
  • 02:00:05 and to listen on these events and do
  • 02:00:08 something you just do the on method just
  • 02:00:11 like the server because the server is an
  • 02:00:13 event Mary you just do on change and
  • 02:00:15 what do you want to do on change well
  • 02:00:18 let's just console.log something has
  • 02:00:21 changed right console.log something has
  • 02:00:23 changed so this is the syntax for event
  • 02:00:28 emitting right now here's your interview
  • 02:00:30 question what's gonna happen when I
  • 02:00:31 execute this code and your answer is
  • 02:00:36 will I see this three times because I
  • 02:00:39 emitted three times and then I'm
  • 02:00:40 listening to the unchanged event and the
  • 02:00:43 answer is no you will not see it at all
  • 02:00:45 why because I started listening after I
  • 02:00:49 emitted the three events right I started
  • 02:00:51 listening after I emitted the three
  • 02:00:54 events and the order here matters so if
  • 02:00:56 we change the order to put the listen
  • 02:00:59 first listen and then emit three times
  • 02:01:01 then you'll see something has changed
  • 02:01:03 three times so the order matter in fact
  • 02:01:05 everything here is synchronous there's
  • 02:01:07 nothing asynchronous yet however what
  • 02:01:10 you can do is you can start emitting
  • 02:01:12 things asynchronously
  • 02:01:14 for example let's do let's do a set
  • 02:01:17 interval and emit something has changed
  • 02:01:21 every second right now we have set
  • 02:01:25 interval that runs every second and in
  • 02:01:28 here we can you know emit some emit
  • 02:01:32 event event emitter dot emit change
  • 02:01:37 right so in here now what's gonna happen
  • 02:01:41 is if something has changed is gonna be
  • 02:01:44 emitted every second and our handler is
  • 02:01:47 gonna call it our handler is gonna be
  • 02:01:50 called so this is our handler basically
  • 02:01:51 we're handling this event by just
  • 02:01:54 constant logging it right so this is
  • 02:01:56 what you use if you have
  • 02:01:58 multiple events going on you would use
  • 02:02:00 an event emitter and here's the thing
  • 02:02:02 everything in node is based on this
  • 02:02:05 event emitter logic so when you get an
  • 02:02:07 HTTP server or when you get a stream
  • 02:02:10 right if they're all event emitting
  • 02:02:15 object right so the you remember when we
  • 02:02:19 did server get and we did that and we
  • 02:02:23 get request and response right we did
  • 02:02:27 request and response and we did
  • 02:02:28 something with request and response
  • 02:02:29 guess what
  • 02:02:30 server is an event emitter request is an
  • 02:02:33 event emitter response is an event
  • 02:02:35 emitter they all have events and you can
  • 02:02:37 listen to these events and you can make
  • 02:02:40 them emit events right can we make the
  • 02:02:45 emitter use async/await instead of a
  • 02:02:47 callback yes you can in fact this is
  • 02:02:50 supported if we the not async/await in
  • 02:02:56 general but an event emitter is also can
  • 02:02:59 be consumed as a generator because this
  • 02:03:02 is this ties into the generator concept
  • 02:03:05 in JavaScript right because JavaScript
  • 02:03:07 has this generator concept so if you
  • 02:03:10 wanna if you wanna look that out if you
  • 02:03:12 want more information about that look up
  • 02:03:14 generators in in JavaScript and have to
  • 02:03:17 consume new event emitter objects in
  • 02:03:20 node through the generators and X all
  • 02:03:23 right so this is all the time we have
  • 02:03:24 for events I'm gonna try to cover a few
  • 02:03:28 more topics in modern JavaScript but
  • 02:03:30 definitely keep that questions coming
  • 02:03:32 because I know there's a lot of
  • 02:03:33 information here I just want you to dick
  • 02:03:36 to get a taste of everything that you
  • 02:03:38 can do in node and really tease your
  • 02:03:41 knowledge into the the capabilities that
  • 02:03:44 you have built in before even installing
  • 02:03:47 anything like Express or lodash the
  • 02:03:50 other big thing that you need to learn
  • 02:03:53 here before I switch gears into the
  • 02:03:56 modern JavaScript just review is streams
  • 02:03:59 so you need to learn about streams in
  • 02:04:03 node and streams are big in them so what
  • 02:04:05 what are streams in general so streams
  • 02:04:10 are like they're kind of
  • 02:04:11 like array of data but instead of having
  • 02:04:15 this data over space you have it over
  • 02:04:17 time right so it's not available all at
  • 02:04:22 once it's not available for you in
  • 02:04:24 memory you're just getting it over time
  • 02:04:26 so every time you have a chunk we call
  • 02:04:29 it a chunk right a chunk of data becomes
  • 02:04:32 available and you can use it in your
  • 02:04:34 stream and after a while you get another
  • 02:04:37 chunk of data and you can use it in your
  • 02:04:41 stream so what our stream is good for
  • 02:04:43 they're good if you have huge amounts of
  • 02:04:46 data that you need to process in your
  • 02:04:49 code if you need to process a huge
  • 02:04:52 amount of data in your code you have to
  • 02:04:54 do it with streams but here's the thing
  • 02:04:56 here's the general advice I'm gonna give
  • 02:04:58 you you should just use streams all the
  • 02:05:00 time even if you have to process small
  • 02:05:02 amounts of data because they because you
  • 02:05:04 never know when the data is going to
  • 02:05:06 grow and you never know what's gonna
  • 02:05:10 happen in that case
  • 02:05:11 so streams are basically just like pipes
  • 02:05:14 yes and you can pipe streams on each
  • 02:05:16 other so an example of handling data
  • 02:05:19 that you should do through streams is
  • 02:05:21 the HTTP example for requests are asking
  • 02:05:26 for an image right if a requester is
  • 02:05:28 asking for a file in general you should
  • 02:05:31 really use streams to handle that you
  • 02:05:33 should read the file as a stream and
  • 02:05:36 write the file as a stream so when you
  • 02:05:39 do that your application will not
  • 02:05:41 consume the size of the file in memory
  • 02:05:43 so let's say you're you're trying to
  • 02:05:46 serve a 1 gigabyte video file in your
  • 02:05:50 web server right if you don't use
  • 02:05:52 streams you will buffer one gigabyte of
  • 02:05:55 data in memory and that's how much
  • 02:05:59 memory your node process is gonna
  • 02:06:01 consume but if you're using streams you
  • 02:06:04 can get these chunks you know 660
  • 02:06:09 kilobyte at a time or whatever the
  • 02:06:11 default chunk size is and you get chunk
  • 02:06:14 one chunk and stream it to the requester
  • 02:06:17 and they get to play while your stream
  • 02:06:20 binary data because it's supported right
  • 02:06:22 video streaming is supported but this is
  • 02:06:24 how you do it you stream at one
  • 02:06:26 chunk at a time you do not send the
  • 02:06:28 whole thing you do not buffer the whole
  • 02:06:29 thing you do not send the whole thing
  • 02:06:31 now note comes stream ready out of the
  • 02:06:35 box so the note the note HTTP package is
  • 02:06:38 stream ready because everything is
  • 02:06:39 streams and all the wrappers around it
  • 02:06:43 our streams ready so when you do duck
  • 02:06:45 send file for example in Express Express
  • 02:06:47 is gonna do the right thing and stream
  • 02:06:49 it you just need to be aware that you're
  • 02:06:51 dealing with streams you're not dealing
  • 02:06:52 with buffers you're dealing with streams
  • 02:06:54 in general and and when you run into
  • 02:06:58 problems you'll run into stream related
  • 02:07:01 problems right you have to be aware of
  • 02:07:04 that so if you get any errors about
  • 02:07:05 chunks and you know stream interrupted
  • 02:07:09 or one stream is slower than the other
  • 02:07:11 right because if a stream is slower than
  • 02:07:14 if the sending stream is lower than the
  • 02:07:16 receiving stream then the streams have
  • 02:07:19 to do it have to do some you know back
  • 02:07:22 pressure kind of thing so there's a lot
  • 02:07:24 of science around streams you just need
  • 02:07:26 to know that you're dealing with streams
  • 02:07:28 all the time you can also create your
  • 02:07:30 own streams in node using the stream
  • 02:07:33 package that comes built-in and you also
  • 02:07:37 get streams out of the box for example
  • 02:07:40 there is the zip package there is the
  • 02:07:43 crypto package that you get in node and
  • 02:07:46 these have functions that work with
  • 02:07:48 streams so if you want to gzip something
  • 02:07:51 or unzip something if you want to
  • 02:07:53 encrypt or decrypt something you get to
  • 02:07:55 do it with streams because all of these
  • 02:07:58 work on can work on large data you never
  • 02:08:01 know if the thing that you want to
  • 02:08:03 encrypt is small or big if it's bigger
  • 02:08:06 than you know a few megabytes you should
  • 02:08:08 definitely use stream this is why by
  • 02:08:10 default you should use stream so there
  • 02:08:12 are a few types of stream but the most
  • 02:08:15 important the most interesting type is
  • 02:08:16 the transform stream the transform
  • 02:08:19 stream so there's readwrite there is
  • 02:08:21 duplex stream but really the most
  • 02:08:25 interesting type is the transform stream
  • 02:08:27 which is what you get when you use the
  • 02:08:28 zip and crypto is where you get one
  • 02:08:32 source one readable source and you write
  • 02:08:35 to a writable destination so you read
  • 02:08:38 from a readable source
  • 02:08:39 and you write a writable destination we
  • 02:08:41 call these transform streams all right
  • 02:08:44 cool
  • 02:08:45 the other thing that I want you to
  • 02:08:47 explore and I'm gonna give you some
  • 02:08:49 resources at the end for what's next but
  • 02:08:53 the other thing that you want to explore
  • 02:08:54 is scaling in general so how to scale a
  • 02:08:59 node application because if you can't
  • 02:09:00 scale another application you should not
  • 02:09:02 be right you should not be running milli
  • 02:09:03 right node has scalability out of the
  • 02:09:07 box like built-in and the way it does
  • 02:09:10 this is through the cluster module so
  • 02:09:12 the cluster module gives you a way to
  • 02:09:15 load balance a few processes together
  • 02:09:18 and you should always run your node
  • 02:09:21 application in production under a
  • 02:09:23 cluster you should not run a single
  • 02:09:25 process even if your server is only
  • 02:09:27 capable of running a single process that
  • 02:09:30 single process should be a cluster so
  • 02:09:32 your server is gonna have is gonna run
  • 02:09:36 as many processes as it has a virtual
  • 02:09:41 cores right so you so for example this
  • 02:09:44 machine and we can look at the virtual
  • 02:09:46 cores here using OS the CPUs which is a
  • 02:09:50 function dot link so this particular Mac
  • 02:09:54 machine has 12 cores
  • 02:09:55 right which means I can run 12 note
  • 02:09:59 processes and the operating system is
  • 02:10:01 gonna be fine with it right and they all
  • 02:10:03 can run in parallel so if I'm running a
  • 02:10:08 production server here I would have a
  • 02:10:10 cluster of 12 processes all running the
  • 02:10:13 same thing and I would have a load
  • 02:10:15 balancer that balanced between them and
  • 02:10:17 the same process is gonna monitor 12
  • 02:10:22 processes and it will automatically
  • 02:10:24 start a new process if one crashes and
  • 02:10:27 we can also do a zero downtime restart
  • 02:10:30 by restarting one process at a time so
  • 02:10:32 we're worst at one process and wait
  • 02:10:34 until that process finishes any pending
  • 02:10:37 requests you know and then when it's
  • 02:10:40 done we'll start a replacement so we
  • 02:10:43 call this zero downtime restart so
  • 02:10:46 there's plenty of things that you can do
  • 02:10:47 and you can do it all through the
  • 02:10:49 cluster module but it's kind of like the
  • 02:10:52 story of
  • 02:10:53 you know HTTP and Express you know the
  • 02:10:55 raw versus rich API so instead of
  • 02:10:59 cluster you can use the PM to package or
  • 02:11:02 something similar PM 2 is just one of
  • 02:11:05 the one of my favorite packages and this
  • 02:11:07 is what we use in J's complete it is a
  • 02:11:10 wrapper around the cluster module and it
  • 02:11:13 gives you rich features out of the box
  • 02:11:16 with just a few commands so that's not
  • 02:11:18 only take a look at PM – if you want to
  • 02:11:21 run a node application in production I
  • 02:11:23 would strongly recommend that you
  • 02:11:25 started with PN – so we just take a
  • 02:11:28 quick look at the big list that we
  • 02:11:31 started with and see if we missed
  • 02:11:33 anything important I'll mention a few
  • 02:11:38 things here but I I kind of want to show
  • 02:11:42 you the inspection it's important how
  • 02:11:44 you debug new applications so this is
  • 02:11:46 this is definitely on the list but I'll
  • 02:11:48 mention a few things here DNS if you
  • 02:11:51 want to do DNS enquiries eval something
  • 02:11:53 should never use write this actually
  • 02:11:55 it's JavaScript path if you want to work
  • 02:11:58 with you know paths on the operating
  • 02:12:01 system so if you want to look up a file
  • 02:12:04 you need to specify a path however paths
  • 02:12:06 are gonna be different on Windows than
  • 02:12:08 they are on Linux and Mac so if you want
  • 02:12:11 to do it in an operating system agnostic
  • 02:12:12 way you do it through the path the path
  • 02:12:15 module here read line if you need to
  • 02:12:18 actually write a CLI command then you
  • 02:12:21 need to take input from the user you can
  • 02:12:23 use this read line method there are few
  • 02:12:26 utilities that we didn't cover but
  • 02:12:29 really it util is kind of going away
  • 02:12:31 because things are coming natively to
  • 02:12:34 the language right so util was just a
  • 02:12:36 replacement but there's definitely a few
  • 02:12:38 things that you can do here what's what
  • 02:12:45 else what else crypto domain don't use
  • 02:12:47 that events again is the most important
  • 02:12:49 net we didn't really talk about net and
  • 02:12:52 net is big so that if you want to do any
  • 02:12:55 TCP programming in node if you want to
  • 02:12:58 do TCP communication and socket
  • 02:13:00 communication you would use this
  • 02:13:02 built-in net library if you
  • 02:13:06 interface with v8 you can use v8 this
  • 02:13:08 this package v8 gives you a few methods
  • 02:13:12 to access v8 directly assert is
  • 02:13:16 something that you get out of the box
  • 02:13:17 which is kind of like a very mini
  • 02:13:19 assertion library that you can use in
  • 02:13:20 your tests so this would replace this is
  • 02:13:23 just like chai for example it has a few
  • 02:13:26 built in assertions that you can use FS
  • 02:13:28 is huge right this is the file system OS
  • 02:13:31 is read information from the operating
  • 02:13:34 system stream if you want to create
  • 02:13:37 streams not if you want to use them if
  • 02:13:39 you want to create streams you would use
  • 02:13:41 this stream package string decoder if
  • 02:13:46 you are using binary information and you
  • 02:13:49 want to decode it into a string you
  • 02:13:50 would use this one Z Lib if you want to
  • 02:13:53 zip things and unzip things oh this is
  • 02:13:56 all built-in you don't install anything
  • 02:13:58 this is all built-in power right which
  • 02:14:00 is really great
  • 02:14:02 and finally query string and URL if you
  • 02:14:06 want to work with with parson and format
  • 02:14:09 URLs all right so there's plenty of
  • 02:14:11 things for you to to learn you got yeah
  • 02:14:13 you kind of have to learn like the
  • 02:14:16 capabilities of all these packages and
  • 02:14:18 of course I skipped everything
  • 02:14:18 JavaScript but there's a lot of
  • 02:14:20 JavaScript in here I want to show you
  • 02:14:23 how to debug a node application so let's
  • 02:14:25 say that we have
  • 02:14:29 we have an array and we wanna a map and
  • 02:14:36 like be equal aid up map and in here
  • 02:14:40 let's say we want to function and in
  • 02:14:47 here we want to return e times e so some
  • 02:14:50 kind of some kind of code that we need
  • 02:14:52 to debug right and console the log B so
  • 02:15:00 this this debugging thing that I'm gonna
  • 02:15:01 show you is gonna work no matter what
  • 02:15:03 even if you have a server running you
  • 02:15:05 can debug node with with the following
  • 02:15:09 so if I'm running this server node
  • 02:15:12 server that is cool things are worth
  • 02:15:16 working so we have a bug here like we're
  • 02:15:19 passing a plus B instead of star we
  • 02:15:21 don't know like what is going on and I
  • 02:15:23 need to debug what is going on why is it
  • 02:15:25 not doubling the elements instead of why
  • 02:15:27 is it doubling the elements instead of
  • 02:15:29 squaring them and I need to debug that
  • 02:15:32 so what I can do is I can run node with
  • 02:15:36 inspect you just do note inspect just
  • 02:15:39 like that node inspect server dodged as
  • 02:15:42 a sorry node – – inspect server digest –
  • 02:15:47 – inspect surfer that yes and this will
  • 02:15:51 this will run the file through the
  • 02:15:54 inspector but this file does not run
  • 02:15:57 forever right because it does not run
  • 02:15:59 forever it will exit so although we run
  • 02:16:01 it through the inspector we also exited
  • 02:16:04 and the inspector accident so what you
  • 02:16:07 can do is instead of – – inspect do – –
  • 02:16:10 inspect BRK which means inspect and
  • 02:16:13 break on the first line so don't don't
  • 02:16:16 exit inspect and break on the first line
  • 02:16:19 so what this does is it will create a
  • 02:16:23 WebSocket server listening on this URL
  • 02:16:26 and you can use a dev tool kind of tool
  • 02:16:32 to connect to this web socket and issue
  • 02:16:34 commands for node to you know break on a
  • 02:16:39 breakpoint or go to the next line or go
  • 02:16:42 you know jump over step over a function
  • 02:16:45 the thing is chrome support this out of
  • 02:16:49 the box all you need to do is go to
  • 02:16:51 chrome inspect and it will see all the
  • 02:16:56 listening processes right here so you
  • 02:16:59 see my node server that justice right
  • 02:17:01 here all I need to do is click inspect
  • 02:17:02 and it will give me ready for a dev
  • 02:17:06 tools for my node script right there and
  • 02:17:09 I can do everything I can do in the dev
  • 02:17:11 tools that I use with my you know
  • 02:17:14 front-end application I can resume
  • 02:17:16 script execution step over go here step
  • 02:17:20 in to inspect the variables local in
  • 02:17:24 this case is one and then step next one
  • 02:17:27 he is two and the return value is that
  • 02:17:29 everything that you have in the very
  • 02:17:32 rich dev tools experience you can use it
  • 02:17:35 with node just with the dash dash
  • 02:17:37 inspect or dash dash inspect BRK and you
  • 02:17:40 can set breakpoints and set expressions
  • 02:17:44 call stack watch expressions everything
  • 02:17:47 right so how cool is that right very
  • 02:17:49 very very cool and every time you pause
  • 02:17:52 in here you can do manual operations on
  • 02:17:55 on the variables that you have so this
  • 02:18:00 is inspection with with nodes just use
  • 02:18:04 the that just use the chrome dev tools
  • 02:18:07 integration with the dash dash inspect
  • 02:18:09 now if you need to inspect a node
  • 02:18:11 application on like a server where you
  • 02:18:15 don't have a graphical interface you can
  • 02:18:18 use the dash you can use the inspect
  • 02:18:20 itself so if you just do inspect not – –
  • 02:18:23 inspect if you just do inspect you get a
  • 02:18:26 terminal debugger so you can debug here
  • 02:18:28 in the terminal and you see how it
  • 02:18:30 stopped on the debugger one I thought I
  • 02:18:32 had it stopped right here and you can
  • 02:18:34 debug everything you can go into I think
  • 02:18:37 you can have to type rebel and now you
  • 02:18:39 can debug things in in the rebel and you
  • 02:18:43 can type in and you can control see to
  • 02:18:45 exit the rebel type and help to see what
  • 02:18:46 you can do in here you can have commands
  • 02:18:49 to break point and continue and
  • 02:18:52 and watch things and all the this so we
  • 02:18:55 call this a terminal debugger and it's
  • 02:18:57 handy if you're debugging things in
  • 02:18:59 production on a server where you don't
  • 02:19:01 have a graphical user interface and
  • 02:19:02 there's a case where it only happens in
  • 02:19:04 that server and it you can't reproduce
  • 02:19:07 it locally so that terminal debugger is
  • 02:19:10 something that you can use as well all
  • 02:19:12 right cool
  • 02:19:14 so there is a lot of modern JavaScript
  • 02:19:17 that you need to to learn if you if you
  • 02:19:20 want to work with node projects and the
  • 02:19:22 reason you need to learn these modern
  • 02:19:24 JavaScript is that everyone is using
  • 02:19:26 them so you look at any node based
  • 02:19:29 project and the project that is gonna be
  • 02:19:32 you know any if you're if you want to
  • 02:19:35 join a team they're probably using
  • 02:19:36 modern JavaScript now I wrote a lot
  • 02:19:40 about modern JavaScript I'm gonna try
  • 02:19:41 and find the three article so in the J's
  • 02:19:45 complete library jobs I think it's
  • 02:19:48 JavaScript for react all the jobs you
  • 02:19:51 need to use before react so I wrote this
  • 02:19:54 for react but it really applies to
  • 02:19:56 everything so I am kind of like focusing
  • 02:19:58 on and this is free you can just read it
  • 02:20:00 here I just complete I'm focusing on the
  • 02:20:02 features that are specific to react but
  • 02:20:07 for node you need all of this and more
  • 02:20:10 so that is the whole subset that you
  • 02:20:12 need to learn so I'm just gonna go over
  • 02:20:15 really quick and just mention what's
  • 02:20:19 important what's really important in
  • 02:20:21 modern JavaScript that you need to know
  • 02:20:22 block scopes are good not very important
  • 02:20:25 oh that we have a list here arrow
  • 02:20:28 functions yes we use this all the time
  • 02:20:30 they are a little bit different but
  • 02:20:32 what's important for you to understand
  • 02:20:33 is how they behave in terms of closures
  • 02:20:36 right so error functions and closure and
  • 02:20:40 this is this article is your first good
  • 02:20:42 step into understanding these but you
  • 02:20:45 get an expand on that after that literal
  • 02:20:47 notation not really new but we we do a
  • 02:20:50 lot of stuff expressions destructuring
  • 02:20:52 another big one here is these
  • 02:20:54 structuring so I'm gonna quickly tell
  • 02:20:56 you about it so this is the D
  • 02:20:59 structuring syntax that we have here
  • 02:21:00 this we use D structuring with arrays
  • 02:21:02 and objects and so this is what Airways
  • 02:21:05 this is with objects destructuring with
  • 02:21:07 objects and its really equivalent to
  • 02:21:10 doing this here so the D structure in
  • 02:21:13 ovaries is equivalent to what I did here
  • 02:21:14 you just take the items as they are
  • 02:21:18 positioned in the array and you give
  • 02:21:20 them local names in the scope in the
  • 02:21:23 local scope you give them names in your
  • 02:21:24 local scopes and that works for object
  • 02:21:30 properties and array items by their
  • 02:21:32 position right so very very very big we
  • 02:21:35 use destruction all the time because
  • 02:21:37 it's a much better syntax you can also
  • 02:21:40 use it in function parameters so you can
  • 02:21:43 D structure a function parameter if that
  • 02:21:45 parameter is an object or an array you
  • 02:21:48 can D structure the properties that
  • 02:21:50 you're interested in right there inside
  • 02:21:52 the function parentheses where you
  • 02:21:55 define the parameters of the function
  • 02:21:56 alright spread and rest the three dots
  • 02:22:00 operator the three dot syntax here is
  • 02:22:03 magic if you use it with destructuring
  • 02:22:05 like this it's a rest operator basically
  • 02:22:08 you're capturing the rest of the array
  • 02:22:11 elements after the structuring the first
  • 02:22:13 one into another array so you use it to
  • 02:22:15 capture part of an array or an object
  • 02:22:19 and you can also use it later on to
  • 02:22:22 spread an array into another array so in
  • 02:22:25 here the three dot operator is spreading
  • 02:22:28 array one in two array two and
  • 02:22:31 introducing more items into already do
  • 02:22:32 and I'm spreading object two into
  • 02:22:34 another object you spread one object
  • 02:22:36 into another object cool so this is this
  • 02:22:38 is the spread syntax another important
  • 02:22:44 things is promises and async await so I
  • 02:22:46 told you a little bit about it this is
  • 02:22:47 more details here about promises and
  • 02:22:49 async await and we also talked about
  • 02:22:52 module important export a little bit
  • 02:22:54 more details here about named imports
  • 02:22:56 and named exports and default imports
  • 02:22:58 and default experts and the rest of this
  • 02:23:00 article here is just not modern
  • 02:23:02 JavaScript but more geared toward react
  • 02:23:06 so in react we use a lot map filter and
  • 02:23:08 reduce we use conditional expressions
  • 02:23:10 and we if you need to use timers you
  • 02:23:13 would need to use set time and set
  • 02:23:16 intervals there's some examples here
  • 02:23:18 about that
  • 02:23:19 so I do want to leave a few minutes for
  • 02:23:21 any last-minute questions but I'm gonna
  • 02:23:23 give you a little bit more what's next
  • 02:23:27 so I have this is not completely free
  • 02:23:31 but I'm writing but I think parts of it
  • 02:23:33 are so I'm writing this complete intro
  • 02:23:36 to node.js which is going to be a mini
  • 02:23:39 book for for you to start with nodejs it
  • 02:23:43 covers a lot of what we cover today in
  • 02:23:44 more details so this is a good next step
  • 02:23:48 if you want to venture into that and
  • 02:23:50 after that I have the node beyond the
  • 02:23:55 basics book which is gonna be a huge one
  • 02:23:58 I'm still working on that so it will
  • 02:23:59 hopefully be published in in the next
  • 02:24:02 few months but this is really the
  • 02:24:04 advanced book after you're done with
  • 02:24:06 just the basics this is what are you
  • 02:24:08 actually gonna learn no this is where
  • 02:24:10 you learn the modules and the
  • 02:24:11 event-driven architecture in the stream
  • 02:24:13 that a child process and how to scale
  • 02:24:14 will cluster so deep dive into the node
  • 02:24:18 concepts that we mentioned today and
  • 02:24:20 there are a lot of them there's a lot of
  • 02:24:22 details now you can learn all of this
  • 02:24:25 through the official documentation just
  • 02:24:27 read the API documentation in fact this
  • 02:24:29 is how I learned stuff is just by
  • 02:24:31 reading the documentation in here and
  • 02:24:34 the examples are not too bad it's just
  • 02:24:36 when you when you put big things
  • 02:24:37 together and work through the context
  • 02:24:40 example that you relate to it's a lot
  • 02:24:43 easier to learn so that's what I'm doing
  • 02:24:46 in the next few months is I am fleshing
  • 02:24:49 out the beyond the basics book to have
  • 02:24:52 like one place where you can learn all
  • 02:24:54 the advanced stuff about node runtime
  • 02:24:57 itself not note packages but no runtime
  • 02:25:00 itself so I don't cover things like
  • 02:25:03 Express or lodash or socket IO I don't
  • 02:25:07 cover any packages I'm just focusing on
  • 02:25:08 what you get built in out-of-the-box
  • 02:25:12 with node and the capabilities that that
  • 02:25:15 gives you so you can build your systems
  • 02:25:17 on top of them because if you can avoid
  • 02:25:20 introducing a new package to your
  • 02:25:22 dependencies you should so you should
  • 02:25:25 not really bring in low – just to summon
  • 02:25:27 array because you can summon array in
  • 02:25:29 three in two lines of in one line of
  • 02:25:31 code in
  • 02:25:33 in modern JavaScript right so you should
  • 02:25:35 avoid getting a dependency unless like
  • 02:25:37 your whole system is designed around
  • 02:25:39 that dependency for example you're using
  • 02:25:42 react you would have react as a
  • 02:25:44 dependency but think so many times
  • 02:25:47 before you introduce the dependency and
  • 02:25:49 if you can do it natively you should do
  • 02:25:51 it natively first all right cool
  • 02:25:54 so we're coming to an end so I'm gonna
  • 02:25:56 leave some time for questions so if you
  • 02:25:58 have any final last minute questions now
  • 02:26:01 is the time we've got seven minutes left
  • 02:26:04 so we have we can we can handle that so
  • 02:26:06 I'll give you a few minutes to type any
  • 02:26:08 questions that you have and I'll see
  • 02:26:12 which bits of JavaScript should you
  • 02:26:13 avoid if in node if possible oh man that
  • 02:26:18 is a big question so what should you
  • 02:26:20 avoid in node and the answer is not
  • 02:26:23 there's nothing special in node you
  • 02:26:25 should avoid bad JavaScript and
  • 02:26:28 JavaScript has a lot of bad parts right
  • 02:26:32 now modern JavaScript solved many of
  • 02:26:36 these bad parts right so you don't
  • 02:26:39 really have to deal with them but you
  • 02:26:43 should for example always use strict
  • 02:26:45 right so this is one thing so use strict
  • 02:26:47 is important so if you go in here by
  • 02:26:50 default node does not use strict by
  • 02:26:52 default so I can define a variable
  • 02:26:54 without var for example and that's bad
  • 02:26:56 you should not do that you should not be
  • 02:26:58 able to define a variable without far
  • 02:27:00 because JavaScript is gonna do the
  • 02:27:02 hoisting thing and confuse your code so
  • 02:27:05 this should not be allowed and you can
  • 02:27:08 put use strict in your in your files
  • 02:27:12 just use strict and this will make you
  • 02:27:17 know the strict mode this will activate
  • 02:27:19 the strict mode out-of-the-box for you
  • 02:27:22 but a better way to just adding this use
  • 02:27:25 strict everywhere is just to instruct
  • 02:27:27 node to always use strict and you
  • 02:27:30 there's a flag for that you can just do
  • 02:27:33 – – use strict and this flag is actually
  • 02:27:35 v8 so if you do – – use strict and you
  • 02:27:40 try to define a variable with that far
  • 02:27:41 that tells you you cannot do that a is
  • 02:27:43 not defined what are you doing if you
  • 02:27:45 want to define a variable you
  • 02:27:46 war or consulate right so this is a form
  • 02:27:49 of protection against bad practices in
  • 02:27:52 JavaScript is to always use strict and
  • 02:27:54 if you're using a bundler like for
  • 02:27:57 example if you're using babel by default
  • 02:27:59 babel is gonna use strict for you so or
  • 02:28:03 if you're using typescript for example
  • 02:28:05 you can tell type scripts you always use
  • 02:28:07 strict all the time assume strict mode
  • 02:28:09 and type scripts gonna give you errors
  • 02:28:11 into using type scripts into using a
  • 02:28:13 non-standard or like bad practices in
  • 02:28:17 fact i would definitely advise you to
  • 02:28:22 explore typescript next because
  • 02:28:24 typescript is gives you a huge value but
  • 02:28:29 the problem is it also comes with a lot
  • 02:28:31 of package for you basically that you
  • 02:28:36 need to you need to deal with types you
  • 02:28:39 need to deal with missing tags you have
  • 02:28:41 to define you have to add write extra
  • 02:28:43 code but give it a few weeks not a few
  • 02:28:46 days give typescript a few weeks and
  • 02:28:48 after a few weeks of working with
  • 02:28:50 typescript you're gonna see the value
  • 02:28:52 typescript is on is next on my list of
  • 02:28:55 things to write about
  • 02:28:57 so we use typescript at JS complete and
  • 02:29:00 it is a huge value for us so in fact my
  • 02:29:03 next Pluralsight course might be on
  • 02:29:05 typescript and now definitely there will
  • 02:29:08 be in the future a book about typescript
  • 02:29:10 so try to use the good practices and try
  • 02:29:14 to have a type checker in your system
  • 02:29:17 but there's also es lint es lint is big
  • 02:29:21 so if you can't use typescript if it's a
  • 02:29:23 big decision for you just start with es
  • 02:29:26 lint and start with recommended rules
  • 02:29:28 for your system so es let is another
  • 02:29:32 code quality tool that you can use and
  • 02:29:35 it's it integrates into your editor so
  • 02:29:37 if you do something bad es lint is going
  • 02:29:39 to complain right away in your editor
  • 02:29:42 and it can also auto fix bad problems
  • 02:29:45 some bad problems for you so definitely
  • 02:29:49 take a look at es lint and configure
  • 02:29:52 your system to have you know the
  • 02:29:55 amended rules like for example a
  • 02:29:58 recommended rule is to always use
  • 02:30:00 semicolons this is recommended rule you
  • 02:30:03 should not write your javascript without
  • 02:30:05 semicolons unless you have in your flow
  • 02:30:07 something that will fix that most cases
  • 02:30:10 for you and warn you if you do you know
  • 02:30:14 if you if you do something like this
  • 02:30:15 because javascript is gonna assume you
  • 02:30:18 want to do that without a semicolon
  • 02:30:20 right so definitely yes lint is
  • 02:30:23 something that needs to be in your flow
  • 02:30:26 even if you're using typescript right es
  • 02:30:28 length is just a different different
  • 02:30:29 category of protection against you know
  • 02:30:33 what you should not be using in your
  • 02:30:36 code so I'm gonna stop at this point
  • 02:30:39 thank you all for attending and see you
  • 02:30:43 in other sessions