Using Node.js with Visual Studio Code
Microsoft Video Courses · 6 lessons · Free on YouTube
Using Node.js with VS Code: 01 Introduction to Node.js
About this course
hello how are you welcome welcome to the visual studio code with nodejs I’m Remy CR and with me is Stacey Mulcahy yeah that’s right are you with me Susan I’m with you I’m calling with you wait
already there’s the one the first the first awesome counter right no that’s too now wait where’s the awesome code anyways it’s counting it’s coming out there we
go Josh so today we’re going to be talking about nodejs and we’re going to be using this really really awesome new tool called Visual Studio code for those of you who have come from another
background perhaps maybe using sublime or some other editors you’re going to find yourselves really really excited about this tool it’s going to be amazing it’s beautiful it is it is like it’s not
my go-to tool every single time I code so yeah a little bit about us hi Stacy hello so I’m Stacy Mulcahy I’m a Technical Evangelist out of New York City I focus mostly on the web stack so
HTML Javascript I dabble on the IOT design and UX you know so I talk about all the things and sometimes I cover even marketing you can find me on channel 9 doing lots of random things
across the board my name is Remy and I’m actually from Montreal so if there’s any Canadians online tonight please please please put some some chat as some A’s in the in the
chat it’ll eat up some maple syrup as well if you can find a way to do that that would be great so actually my focus is very similar to Stacey’s I focus on the web html5 node
and right now I’m super into Microsoft edge working on getting those web standards there and showcasing some of the really cool stuff we could do with Microsoft edge in Montreal I like to
help out different developer communities I speak a lot of different conferences and I work a lot with startups as well and if you’re curious about some of this stuff
I’ve been working on it’s all on MSDN so that’s definitely the place to go if you’re interested in html5 articles nodejs articles it’s all up there so today we’re going to be covering a bunch
of different things in terms of node and kind of showing you Visual Studio code and some of the features of Visual Studio code and so what we’re going to be covering today is we’re going to be
doing an introduction to no js’ so this is going to be taking you through the very beginning of nodejs we’re going to talk a little bit about you know introduction to express very popular
framework that you end up using with node we’re going to talk about expressing databases we’re going to also be doing a little bit of the front end for your Express web apps you know
explaining things like bootstrap a little bit and Jade and how they kind of relate to node and Express we’re going to go through to bugging and deploying nodejs
and we’re also going to be talking a little bit about oK you’ve been using this mostly throughout the day to build websites we’re going to talk a little bit about how can you actually build
something that’s like a you know like a cron job or an as your web job and how can we call it continuously and what kind of things can you do there so you know having a little bit of fun I think
towards the end of the day in terms of expanding your knowledge yeah to set some expectations ideally you guys might have had some experience with web development if you’re a web designer who
knows html5 CSS you’ll feel very at home we’re going to walk you through know jasperse right from the beginning so if you’ve seen or used or played around with JavaScript you’ll be great
and of course any developers with experience using other service side languages like Python PHP Ruby you’ll actually really enjoy this tutorial today now of course there are some great
great other resources out there among them mastering nodes is this free book that you can find online and if you’re curious about how to set up nodejs on your Windows machine or if you just want
to read a very lengthy article that explains all the different pieces of nodejs you can go to that short link at the bottom AKMs – node about No – 101 and will be a great article for you so
again taking this course you can earn some points and so if you enter the code nodejs Visual Studio expire on the 30th of August you can get your point 50 MVA points for this and
you know again this is got kind of be a follow-up to our previous one that we did so this is going through a little bit more of visual code so get your get your MVA points while you can I guess
yeah the best way to put it all right let’s get started so introduction to know Jess I guess in this module what we’re going to cover is the basics of node what is node when was
it started we’re going to show you how to set up your environment if you’re on a Windows machine if you’re on a Linux distribution or even OSX we’ll will show you how to get that started up and
running and then we’re going to build our first node application so we’re going to walk through the different parts of node and how it’s a little bit different from JavaScript and then once
we bought a couple of great applications that we’ve built we’re going to talk a little bit about the node package manager which is phenomenal it’s the the single thing that I like the most about
the node community it’s the package manager seeing all of these packages out there and helping you build your projects really fast and really quickly it’s magic it is it is magic it’s like
all of a sudden we bought all of these things that like just do half the work for you but really your job at that point is to just make sure the packages work correctly and to put them together
yeah it’s great yeah now if you want to check out some of the code that we’re going to be playing with today it’s actually on a github repo already so you can go get it you’re
right there you you might want to pay attention so as we’re doing the tutorials we’re going to be going through all the different folders but we’re going to be perhaps jumping
between some of the folders and just pay attention during the video so that you know which folder were actually in and then yeah so why don’t we get started yeah artists so let’s talk about notes
first what is node ah it’s like you’re asking me the open question yeah you know jazz has a runtime environment and library for running JavaScript applications outside the browser and the
nutshell that means that if you’re a JavaScript developer and you’re used to doing something on the front end you now can take that knowledge and start doing things on you know servers are back in
notice most we used to run real time applications and shine through performance using non-blocking i/o and asynchronous events yeah and that’s actually really powerful because
traditionally that’s been always a complex way to build applications like real time applications you know you have to have this loop of some sort that would keeping track of time and whatnot
and it was fairly complicated but node makes it fairly straightforward I think I think that’s one of the beauties of node is again we’re talking a little bit about package managers and things like
that is that if you’ve never touched it before you come to it you start to realize all there’s all these things there’s all these Lego building blocks that I can start to use and just bug in
and play and so like for me that’s one of the great things coming to node yeah so node if you if you haven’t ever seen before it’s it’s a great platform and it uses JavaScript and it’s most
predominantly used on the server side but now we’re seeing it more in IOT devices pretty much all over the place anytime that you’re seeing different platforms even for mobile apps you’re
starting to be able to use javascript there now what’s really cool about node and javascript if you’re building for the web is that you really have a unified development environment and the
same language now note itself all right we said that it’s a platform that uses JavaScript and essentially it came about because JavaScript which for a long time was fairly slow has finally been
incoming high performance and in fact lots of different browsers have had very high performance JavaScript engines and that’s what really allowed us to take JavaScript from the browser front-end
side and actually put it on the back end where it’s able to handle the load and in node in particular it actually uses the v8 JavaScript engine and note itself was created in 2009 by Ryan dalla it’s
an open-source technology so you can go check out all the code that’s behind node you can see its evolution over time and it runs on Windows Linux OS X it pretty much runs everywhere actually
including little IOT devices like raspberry PI’s and whatnot yeah now you might think like hey I went to the node page and like what is this zero point something like version number like what
it what is this well yet I I guess you could say that note is in beta phase but not for a lot yeah like a lot of things you could say that for yeah
but really it’s actually been used in production in so many different places now that I you cannot call it beta but like I guess it’s not 1.0 yet because people still want to the developers
still want to change things to be flexible and and really a healthy language and platform evolve quickly so if you’re wondering well when do I actually use no yeah when do I use it
yeah how about a minute tell you notice great for streaming or event based real-time applications and we mentioned this earlier but like what does actually mean I don’t know tell me okay seems
like chat applications right it was our event base there’s a chat that was sent chat that was received right they have to be real-time as well in the sense that if you’re chatting with someone you
can’t wait like 10 minutes to get the next shot yeah I wonder where’s the browser you know doesn’t make sense yeah what are you doing there yeah from your message
yeah Yeah right angler so you’re typing you can also use it for real-time application so think like anything that requires some some type of data flow constantly going back and forth so if
let’s say that you’re building I don’t know financed websites or something right or like it in stock tickers or something or even a shared conference event or something where
someone’s you know doing something at the same time as you and you have to make sure you’re not overriding it or whatnot yeah and there’s lots of people that are building collaborative
environments on the web with nodejs and that’s a better way to put it yes yeah flowers are things that we’re people work together yeah yeah multiple browsers talking at the same time to the
servers and all this information is flowing around that also brings up the idea of game servers right yeah right so I’ll tee you there yes also user games yeah you can also also see it interrupts
and add servers that you don’t have to do this high volume amount of requests okay streaming servers as well so it’s really great when you need high levels of
concurrency so when you have lots and lots of lots of small things happening at the same time gotcha but you don’t need as much hard CPU right like no no heart superpower you
don’t need like let’s do these complex math calculations that you know take like 10 seconds even on our very advanced hardware yeah you know is that’s probably not where you want to
use node right but for everything else that’s related to the web where you’ve got high concurrency needs where you’ve got lots and lots of users happening lots of events happening at the same
time and you still want to be a real-time node I think is the best tools one of the best tools out there right now and of course it’s great for just writing JavaScript code everywhere yeah
yeah I mean why wouldn’t you want to have it everywhere yeah like it’s it’s taken over it is everywhere and if you’re wondering like where is it in the wild well we use it at Microsoft yeah in
fact a lot of our cloud tools are built with node yep there’s other companies out there what’s interesting is that some of them are also contributing to the node community
lots of open source packages that are being produced by different companies around around the world and they are all being open source which is absolutely phenomenal for the node community yeah
and you’re starting to see even like SDKs or you know maybe considering node is one of the first things that they’re creating an SDK for or whatever it may be along those lines so you know it’s
being embraced by everyone yeah and speaking of that community it’s huge now yeah huge oh man it’s like I mean these stats are probably out of date now they’re totally out of date holy Haddad
a bit like built the last time that we had collected stats for our slides there was over 2 million downloads per month it’s probably a lot more than that now last a major version relating 0.10 there
was 20 million downs of that over 81,000 modules on NPM so these are over over like that means is over 81,000 pieces of code that you can use in your application it gives you time it saves
you time ideally yeah and but of course I haven’t seen any no application use all 81,000 it’s a challenge like I just threw that down there just like if you can build an app legitimate ly use it
every single module is out there yeah I will be more NBA point for you more in via question and of course there’s plenty of Europe’s around the world Stacy you’re in New York and there’s
probably several node meetups yeah we have a bunch and they are again quite regular once a month and we discuss like all sorts of things across from now then you mentioned like different things that
you might use it for and some people might use it more the utility to do you know things off the fan line some people are building like a full and you know back-end server websites that kind of
thing a lot of IOT stuff so it’s interesting because you’re really seeing it kind of you know expand where people are using it and so I think that’s the most interesting thing for me watching
the community grow and at least in the past couple years for sure yeah it’s the same story in Montreal we’ve seen a lot a huge community grow around lots of startups are using nodejs as their main
stack yeah and it’s just it’s just phenomenal lots of contributions lots of people lots of players everyone’s really happy when there’s a big community right thank you yeah the means are going to
get your answer yeah basically but when you got a question you lost people locally who can guide you through that yeah and I’m in Montreal so if you’re ever
interested in talking about know just feel free to tweet at me and yeah so let’s let’s get started let’s start with setting up your environment yeah yeah so if you’re on Windows there’s a couple
ways that you could do it you could go straight to node.js org and download these installer files and we’ll install the nodejs binaries for you that’s how uh if you want to build it
yourself you also can yeah although it’s a little bit more complicated your purist Europe you a few the need to compile things and that the need the need you need to compile things I know
maybe you’re looking an excuse to do some sword-fighting in the meantime oh I think you can definitely do that as well if you use chocolaty just like a package manager for Windows you can do chaco
install nodejs install and it should install node.js for you one thing to keep in mind and something I always recommend you double checking if you’ve got note installed and ideally you want
to have no the executor will file in your path so you got to make sure that it’s actually in the system environment variable and I left a little link there for
YouTube video that shows you how to get to your environment variables in Windows 8 and 8.1 with 10 launching today today today today you also want to do that as well and just make sure that this
variable this path at the bottom right here of the slide program files whatnot is in your fat variable that way when you’re in the command line trying to execute some node apps it will actually
find node let’s say that you’re not running Windows if you’re on a Linux distribution perhaps that Ubuntu it’s a very famous and popular one there this it’s also the same similar to so we know
the easiest way to install it is with a package manager and we’ve got the command right up there you probably also want to install some of the compilers and build essential tools because
there’s a couple of node modules that do use some more native stuff so you’ll want to run both of these commands app get install build essential and again for nodejs and NPM yep let’s say you’re
on oh s X again the easiest way to do it is via the terminal using the brew package manager it’s also how I do it as well and let’s say that you don’t want to you can always compile it from source
or use the Installer on Neches org so let’s kind of get down to a very first initial node application yay moving on yeah how long alright so basically with every single language and every single
platform the first thing that you want to do is just say hello world I’ll write out or hello node what we’re going to do is we’re going to switch over to our code editor and we’re using Visual
Studio code and this is a and the best editor in my opinion and you can run it on OS X Linux Windows I’m running it on Windows here but you can run it on your own platform as well node will work
everywhere as well so let’s get right down to it so here I am I already got my folder open this is the same node env a folder that you will get when you go to github alright so if you
get this clone to your computer you’ll actually you’re actually able to open up the whole folder straight into individual city code and the first thing that you want to go to
is the app J’s file in the 0-1 hello world folder and this is pretty much how you do hello world it’s quite simple you just want to print it out you can say console about log of the world very
similar to how you to do it in the browser sure so instead of going into the inspector and by clicking and seeing it it was actually gonna you know popular
popular so when I run it so I’m going to open up my terminal here and did I lose my keyboard I hope no no okay here it is let’s say that I wanted to go into that folder first to run it of this very
simple app dogs as well I just type in node app KS execute and there we go hello world yes this is awesome okay we’re done all right great now let’s let’s say that
you know we wanted to actually do something a little bit more relevant right note is a web platform technology right let’s say we wanted to do basic make a basic HTTP server that will then
do hello world and send that to our browser whoo letting it up yeah what’s one plus one right okay so let’s go back to the code here and what we’re going to do is we’re going to take a look at a
second folder and I’m going to walk you through every single line here and it with a couple of lines this is about six you’re actually able to respond to an HTTP request at reply with a 200 okay
that this is a good request and write hello world back in the response so the first thing that you’ll notice is that in line one we’ve got this that require function and if you’re coming from the
browser world you probably have never seen this before little odd right what does it do well basically it actually gets a module for you I always want to ask you the question
what does it do tell me tell me more so it actually will load up a module and in this case the module is HTTP right right and in this module there is a bunch of different servers clients for HTTP
protocols right so the way that packages work and we’ll cover this a little bit later is that once you require something it will return everything that’s exported in
into a variable and in this case we’re sending it to HTTP do you think you could make the font just a little bit bigger yeah I’m trying to I’m going to do that right now for you I’m just going
to try to remember how to actually do that consume any – okay well maybe we should run zoom it we’re just going to make that a little bit bigger so that we can read it oh okay there we go that
better yeah yeah yeah we got the thumbs up that means it’s better okay great so let’s take a look at this a little bit more we’ve got the require it’s put a bunch of modules and function into HTTP
right and if we want to call some of these functions we can just use the HTTP variable and obviously the first thing that we’ll want to do is perhaps create a server for responding to our HTTP
requests all right so in line 3 if you can see right there we’re going to say HTTP dot create server then we’re going to pass in a function now this is a callback and
we’ll cover callbacks in a few seconds but essentially what this will do is that for every single HTTP request our server gets it’s going to execute this function and this function is going to
be passed to parameters to arguments requests and response right so once let’s save that so we’d get a request with the thing that we’re going to want to do is we’re going to respond with a
200 ok so we’re going to say response that write the header of our HP response with the status 200 and we’re going to specify the content type text plane now if you’ve ever take a look a look at an
HTTP header you would have seen there’s plenty of other headers that you can put and with this object here you can actually specify a lot of them but for our case we’re just going to use text
plane normally you’ll want to use HTML of course if you’re sending HTML back but in our case we’re going to keep it very very simple and just use plain text and before we end the response we’re
going to say hey response stop and hello world once this is done this is going to send a response back to our browser that’s going to then basically a display hello world ideally and now
the thing is when we create this server we actually don’t tell it to start yet we got to tell it to start by calling the listen function so HTTP loc create server is going to return in an object
that’s we’re going to put in the variable server and then right here out one line eight we’re going to go server that listen passing the port number that we want to listen to 7,000 and then if
we get this started and we hit our local host on port 7000 it should say hello world Oh like the amount of truth the moment of truth let’s do it so I’m back in my community and I’m going to go out
and go into our second folder right here and do a node fjs again and nothing has happened and that is actually the correct behavior because if we pay attention at the top node address is
still running now what we’ll want to do is we want to open up our browser all right and we opened this page let’s say that we’re going to go to 7,000 right there boom hello world can you believe
that this is awesome yes yes and I’m going to open up this tool just to show you a little thing right here if I go into my network tab and I’m gonna refresh this there you go it actually
did send text plane okay what the 200s which is exactly what we were doing perfect awesome so all it did oh we did all that in six lines of code yeah I don’t know that’s a
that’s one of the part of the magic I guess I’ve known like you can do so much web stuff in very few lines of code yeah I mean it for programmer productivity requiring that modules module does all
the work for you you just have to understand how they’re like leverage it and that you’ll that’s going to be a theme throughout yeah absolutely we’re going to show you how to use some of
those popular modules as well so now let’s say I guess we touched on a couple of interesting things like a callback yeah there’s some events and whatnot yeah let’s talk about event-driven
programming event-driven programming I love that so event-driven programming you know and just on the screen you’ll see programming paradigm in which the flow of the program is determined by
events you know such as you their actions we’re kind of used to this I mean especially if you’re doing with JavaScript you’re very used to capturing a click or understanding when there’s a
mode center or any of those kind of things and so that’s very much in all along the lines of of what we’re doing here in terms of events so we can listen to a stream starts it closes that kind
of thing yeah an HTTP request got sent that’s an event as well and now you’ll you’ll notice that we never actually started an event loop perhaps like there how does this event system get started
right well it turns out that node provides the event loop as part of the language so with node you never need to call start to the loop at all and the way that it works is that the loop
starts and it doesn’t end until the last callback is complete so if you have a callback like we did when we created our server for HTTP and as long as there is at least one callback the event loop
will continue executing and the program the node won’t won’t stop right all right so the other thing that you need to know as well about this event services all run under a single thread
so if you do some very long operations like say a very complex math calculation or something as simple just saying sleep yes and seconds you’re actually going to halt the event loop and prevent all the
other callbacks from being executed right yeah and that’s going to bring some other interesting topics out as well like how do we write code that doesn’t block the event loop because
ideally you want the events loop to be fairly clear so that you can handle thousands of different events at the same time right and anything that’s blocking that takes too long we want to
put that off to the side yeah all right so let’s talk a little bit about what blocking i/o is yeah yeah so here I have a very simple example and the way that I’m explain it is by using some actual
code so you can see what it looks like try it out yourself what we’re doing here is we just want to read this file called package JSON which we’ll also talk about a little bit more in detail
further on but it’s for now just assume it’s a regular text file now if we wanted to read something in node we’re going to have to require a module and in this case FS which is file system we’re
going to say hey require file system put it in this variable var FS FS we’re going to say read the file right I want to call a read file function standard IO like standard file I entered
I oh yeah it’s going to read the whole file before it does anything right and this is going to be the way that we specify if it’s a blocking i/o is by having the sync at the end so
synchronous right and it’s going to come in a little later a synchronous versus synchronous right and now if you look at this line okay will do is it’ll actually read the file synchronously and then
convert it to a string that’s going to put into the variable content and we’re going to print out that content so the code is actually in the repo if you want to try it out but we’re going to talk a
little bit instead about how we do that same time of operation in a way that doesn’t block right because I mean if it’s you’re doing it the previous way you’re waiting you’ve got nothing
there’s no input it’s you’re blocked out completely let’s say that file was a gigabyte long that’s going to take a while even you know like yeah yeah billing of sums there so here’s the
thing io is very slow but it’s much slower than CPU it’s much slower than it’s typically my slow in and specifically disk IO is very very sad even with us as
these is still slower than running and writing to memory reading from memory doing CPU or CPU work so ideally what you want to do is you want to have things like file reads or even network
network i/o okay you want to make it in a non-blocking way so that you can still do some other work you can process other events you can do other things and that’s really where the power of no des
comes in because it makes that so easy and the way it does that is by having asynchronous functions that take a callback so let’s say we wanted to read that same file again if we take a look
at the screen we’re going to call read file instead and we’re going to pass in the name of the file but here we’re also going to pass in a callback that returns a buffer so what know is going to do is
know is going to execute this file actually this line of code is going to continue executing other code until there is enough things in the buffer for it to call the callback right all right
so what’s going to do here is once it gets enough things in the buffer it will call the callback and we’ll print it out the screen and then that way doing it this way you can be
doing anything else at the same time capturing anything else or another operation or that kind of stuff and all you care about is when is this what am I getting this event when does this call
back being called when can I handle it so that brings up the topic of call back style programming right so if you’re coming from another type of programming style let’s say imperative or
declarative and other languages you may not be particularly used to callbacks essentially when you have an event loop that’s so crucial especially like in node where the event loop is the main
reason why you want to run node right you want to be able to handle all this concurrency event loops typically result in callback style programming and your end objective really is to break apart
the program to its underlying data flow to the underlying IO that you’re doing so if you’re building a networked application you want to make every single callback almost the same length
as the actual IO that you want to do so that you can that way you can do other things that other callbacks that are that don’t take up as much time so you want to split up your programs and small
and small chunks until each chunk is mapped to an operation with data so you don’t freeze the event loop on long-running operations like disk and network IO right now of course I’ll call
backs within callbacks within callbacks might lead to something that hey heard all backside yeah I’ll back in your callback yes quite quickly you’ll often find yourself writing code that starts
look like this it’s like the pyramid like a pyramid structure and it’s not you know fantastic but there are ways to candidate and will let you explore those ways as you dive deeper and deeper and
deeper into node yeah just you know it’s more just be aware that this might happen to you and it’s very natural because you’re you know just trying to step on what you’re writing and
everything just kind of falls in oh when this get callback I need to do this in this and then before long you’re gonna be like okay I can’t handle this I can’t read it
it’s not manageable I can’t follow what’s going on anymore and so there’s a bunch of demos that you can or sorry libraries that you can use along those lines to kind of help you with that a
little bit so you know don’t don’t don’t be too worried that happens yeah now I want to do some some more coding right and I’ve got another sample for you I’m going to
introduce some other interesting topics in node and what we’re going to do is we’re gonna go to our folder five and then check out our hello world TCP project and here what we’re going to try
to do is going to do hello world again but this time using the TCP protocol so not HTTP now the first thing I’m going to want to do is that maybe perhaps create a TCP server now this will look
familiar it’s almost exactly the same as the HP one but in our case what we’re going to do is going to require net which is where all the TCP handling modules is that’s the bundle that you
want to use and we call net crate server but in this case our callback is going to take us socket and what this socket is going to do is it’s going to give us an address and it’s going to be able to
receive and send data right right so what we want to do though is we just want to send hello world to anyone who connects so our socket is only going to print out the remote address from where
we got our connection from and it’s going to write hello world through that socket again we’ll assign this server to a variable and then call listen and give it a host where it’s going to run from
and in this case it’s just localhost just home and we’re going to add start it so if I open up a command prompt right here I’m going to close this server we’re going to go out go into 0 5
all right I’m going to call node server dot yes all right there it is it’s running at the top in the meantime I’m going to actually open up a another command prompt and I’m going to do that
by right clicking putting it here I’ll put it side-by-side so you can see what’s going on let’s go back into our dev folder go into our TCP and I actually want to create the client now
so before I actually run the code that we have I’m going to walk you through it a bit so you can see hey this is actually a little bit different from how we do our server now at the top you’ll
notice the same idea required a module what we’re going to do here instead is we’re going to clear a socket and then we’re going to assign this new socket to the client variable and then we’re going
to tell that client to connect to 7,000 localhost that’s it there was no callback that we did here instead what we were going to do is want to say hey any time the client gets data then do
something right right or any time if the client gets a close yeah like someone close the socket do something so big we’re just listening to certain events there yeah we limit so we’ve talked that
about event-driven programming already yeah and in this case is a very simple example of literally events data and closed right it’s that’s that’s it’s right there and this is going to bring
up this topic of event emitters so these are node constantly node concept where we have these objects that are emitting events all right and in this case the socket object is one of them so now if I
actually run this code I open up my two command prompts I’ll on client say yes what we got a connection on our server from our own home I have we in our server we sent back immediately
this data on data in our client s forget we printed out set data hello world now if you remember if we look at our code again the server JS file does close the connection right at the end it called
socket end and that’s going to call it that’s going to call the closed event in our client and then it’s going to print out connection close which you can see right here perfect yeah all right
awesome so event emitters let’s talk about what actually happened here event emitter is basically event emitters allow you to listen for events and assign functions or callbacks to run
when events okay and of course each emitter can emit different types of events so in our case we saw data we saw clothes but there’s a special event that typically is available and that’s the
error event and all it does is it it lets you know when there’s an error in your event in there if you want to read more there’s a couple of tutorials on the on the screen right there or in the
slides so you can check out and that’s take a look at them now of course if you’ve got all these events and it seems like we’ve got the stream of stuff going on what is a stream can
we actually use it to do something cool yeah and you know what what do you use them for and what context and I think most often for me reading or using streams is usually I’m going to be maybe
I’m like downloading something or creating a stream where I’m like just filtering that information in and like you know a good example fouling an image and piping it into a temporary kind of
placeholder or that kind of thing so there’s a new class of called streams and it represents data streams like I said so some IO stuff right perhaps network or disk IO maybe even
streams of data afirma from a message queue or something now these streams can be piped together which is pretty neat just like in UNIX if you’ve ever used a UNIX based I don’t
know how to say this I think it has some you are mining it pretty good you’re like you know when you do this with it yeah I got it from one to the other so if you’ve ever use a UNIX type system
perhaps OSX or Linux you’ve seen the pipe operator that’s a great line that you can put in your in your code and just move output from one and check but at the input for another one you can
actually the same thing with node so let’s say we wanted to read a file package.json we can actually pipe it straight to a write stream that’s going to rewrite the entire stream that it is
in package JSON to out JSON right so it’s just bringing in and writing it out yeah pretty cool stuff no we keep talking about all these modules and and stuff like that so how
does it actually work yeah well I mean no js’ has a simple module on dependency loading system and so in that sense it means that you can you can create your own modules you can use other people’s
modules and just being able to easily install them whether it be locally or globally you know you can start to use them yeah and it’s very similar to the I guess UNIX philosophy we have the same
type of philosophy in the sense that modules are typically small and you you want to write programs that do one thing and do it well yeah and know do you want to write modules that do one thing and
do well yeah you see a lot of it for like working with api’s right like you’re going to work with a Twitter yeah there will be like a bunch of module for that you want to do something
that you know something working with date or time you’re going to have a module for that so it is very much that concept of like doesn’t do everything it’s it’s utility based making your
tables and thousands of modules that you could use now as we mentioned a couple of times already how do you actually load a module well you use the require function you can actually give it the
path of a file so you can require another nodejs file or even a directory that contains the module that you would like to load and everything that is exported from that module is then put
into a variable so now let’s talk about the new package manager yeah we we like these modules and we know that there’s a package manager that has all of this well what is the NPM yeah what is a NPM
that’s like every time you know you start working with no would you like NPM what is it well fishel package manager for node right and so it’s the idea that you know
everything is listed there there’s a registry you can find out even though you can go to the NPM play figure out what modules you can get and it’s you know does all the hard work for you it
bundles and installs automatically within your environment and it takes care of all the dependencies so if you are using a module that requires a couple other modules for whatever reason
you don’t have to worry about all that dependency stuff and managing that which we all know as things change and move and get upgraded can kind of you know be a little bit of I don’t know hairy
experience right but it handles all that for you and so if you’re looking at you know working with NPM which you will undoubtedly you’ll be using something along the lines in terms of usage you’ll
go like NPM install for example that allows you to install the module name you’ll just need to know the name and then you can do the dash dash safe and that will actually lift it you know
alongside your project in a lifting that we’ll get into called package.json and we’ll kind of keep all that sense of what you are using and when but again you can you can install these modules
kind of locally and you can also install them globally so there might be something you want to use all the time for example I mean also you use things like that or NPM update to update you
know its registry listing so you have the most current for example yeah so how does it actually work well whenever you do an NPM install it will actually install that pack
age at which you can call it a dependency if it’s a dependency free project into a node modules folder that sits locally right next to your package JSON and the current folder that you’re
working with yeah now if maple Oh folder there’s a little folder right there no modules it actually did something interesting it actually checks to see if there’s a node
modules in the folder above all the way up to the root and the reason why it does that let’s say that your three folders deep into your project and you do an NPM install there it actually
doesn’t want to put it another node modules in that folder I’ll actually put it in the top one which brings this interesting question what about modules that we want to use all the time yeah
well in that case you can actually install with the global mode set and that will and make that node module accessible to all the products that you have and how can I install and from
where there’s a couple of ways you can get it from a folder from the tarball from NPM itself like using the web whenever you do npm install the package name it’s actually going to the web to
give it a folder or a tarball will go there and of course you can specify things like Devon optional packages that you might want and that will all go into the package dot JSON so whenever you
download a node project the first thing you typically want to do is NPM install enough in that folder what that’s going to do is going to read the package JSON files for any of the pendency that
project means yeah let me go fetch them yeah but I’m in the local node um underscore modules folder and can go fetch them yeah off put in a cart bring it back for you yeah and then it’ll make
the project ready for you and now let’s actually take a look at what’s in that package JSON it sounds very important right yeah I mean you know it’s a listing of all of the modules that
you’re going to be or dependencies I mean we can just say dependencies at this point that you are using in your project and it will list everything from the name of it to the version
description so you can just look at this open it up and be like oh I’m using one point zero one six five I don’t know of this module okay I got to be very careful if I’m going to update it or any
of that kind of thing and when you create a module as well you start to lift those things along those lines whether you know there’s a github repo an author and then
in the description all that kind of stuff and again it’s just a simple JSON format that just fills out all those dependencies within your project yeah and what are some of the popular node
modules I mean there’s well for me it’s a I mean I think there’s like requests we’ve got underscore we’ve got a sink we’ve got low – commander right Express I like how you just queued up that slide
that was beautiful my friends you know optimist CoffeeScript I mean there’s a bunch of really popular ones and you’ll see when you go to use one you know just check out like how active it is and how
many people are using it or downloads and that’ll give you a good indication of you know if you want to pursue something because I obviously you know specific oh people put up and if they’re
not maintained you you know at your own risk right yeah absolutely typically most of these that modules are on github so you can actually go check out the code yeah and good way to learn
we want to highlight two modules that are very very useful and they’re extremely popular as well yeah the first one is async and essentially what async does it’s a utility module that gives
you very easy simple ways to make functions work in different ways when it’s doing their asynchronous right right because when you have asynchronous JavaScript you’ve got lots of callbacks
perhaps you’ve got multiple functions that you want to run in a series well it’s a little bit annoying to have all of that pyramid of of callbacks but perhaps you want to run them in series
well async provides a bunch of functions that let you do that and I’m going to basically show some some of the code real quick right here that first one at the bottom is async thought series so
that lets you run an array of functions parallel lets you run all of these functions the same time and then when the results are ill or when the first one is done it’ll execute that callback
that you specify but there’s two other powerful ones that we use a lot like map and filter and basically what map will do is it will run this this function FS stat on every single argument that’s in
this array and it will return all of the results in an array for each file and so that you wanted to filter while filtering it’s very similar in how it executes it will run all of this stuff
and this FS exists function on all of these files and it will return it all in the results and there’s lots of other functions that async offers another very popular module that Stacey will walk us
through is the request module ya request ism it kind of simplifies the whole way of doing any kind of HTTP calls and you have to do this all the time maybe or you’re getting a page maybe you’re
getting a an image that kind of thing so you can see the structure listed here on the screen and have a couple of examples that I can very quickly walk through and so I’m just going to open up code on my
desktop and again if we’re looking at this I have a bunch of examples in here and you’ll see that at the very top I’m just you know including that request module again I had installed it at some
point whether I’ve done it locally or globally and I also including FS which kind of does all that file IO stuff and at the very top right up here all I’m doing is a simple request thing you just
pass at the URL and then you’ve got a callback and it tells you okay I’m going to that URL I’m going to get the response in the body from this page and I can see you know was there a response
status code you know if there is just log the whole body if everything’s good and you know it’s a 200 and it’s just going to write back that HTML so that’s the idea of like just you
know grabbing the content off a web page very simply but I have a couple other examples here and I’m just going to uncomment this out and I’ll run this one maybe in a second a minute yes this is a
good one there’s no something to do with the owner that’s one of one for you my friend so you know if we’re looking at the bottom here you’ll see that what I’m doing is I’m basically saying okay I
want to create a write stream I’m going to write an image and I’m going to go get that image and I request this image and I’m going to pipe it into this basically this file that I’ve kind of
this placeholder the stream I want to pipe it into and I can also capture when it’s done so I know that when this operation actually is finished piping it in I have a sense
of when that is going to complete and so what I can do here is I’m going to bring up my command prompt and I’m going to go into get a and I’m going to go into oops
CD this is where you get to watch me type and it’s really exciting alright not prepared CV node and there we go and now I’m going to do this okay so node and then it should be apt jf
okay so it gives me rights back to the thing okay we’ve got the stellar pick of Ramy I’ve basically gone to the web I’ve went and I’ve downloaded this pic of Rami that you see right here I feel like
maybe we should take a look at it I feel like it would be excellent to go and do it don’t know what this picture is so it’s all good yeah but you know again if you want to go for example and grab a
couple pictures and download them locally and you know whatever you might be doing that’s an easy way and you can use request to do that I see that you piped it into the file stream yeah I
totally did the whole pipe right into it and there you go you’re welcome so there’s our request module demo we have a couple of resources for you to kind of move you along in terms of
learning and other things that you can take a look at we’ve talked about a lot of concepts in this first you know hour here and some of them you know you need to get a little bit maybe working with
know and building your stuff and you’ll understand how they come in plug and play and what you need to look at but again we’ve listed some resources we’ve got the github repo there really if I
think after this one hour things that people should probably kind of look at I would say understand a synchronous versus synchronous get a really good handle on that because man when you
don’t it’s just like it comes up at you and you’re like where did this come from and all of a sudden it’s a day of learning libraries like you or async you know get get very used to vent
emitters and and how they work and NPM learn all the ins and outs of NPM and then and then come tell me who’s there you know yeah so you know that’s them that’s all we have for this first module
and we’re going to take a short break and we will see you shortly yeah and if you’ve got any questions please put them in the chat and we will cover some of these questions when we
come back last module yeah we’ll do that yeah awesome awesome
THERE IS ALWAYS MORE