hi everybody my name is Logan Huskins
and I guess just a little bit about me
before we get started I am a software
developer I work at love's travel stops
I've been writing node at work pretty
much exclusively for the last nearly
three years married my wife showed up
that's pretty exciting
glad she gets to see me talk I also
helped run oklahoma city in oklahoma
city mechanical keyboard user group so
if you're into my kind of keywords hit
me up and we can I can show you where
all that stuff's at it's pretty it's
pretty pretty dorky but I have so my
plan with this talk is basically I'm
gonna give some height as I'm really
like high level concepts on some stuff
and if it's complete another nonsense as
most of it will be initially I will
circle back to pretty much every bullet
point I have and kind of explain more in
depth what those things are but if you
have questions before that if you have
questions after that just kind of wave
at me get my intention I'm be happy to
answer as many questions as I can so I
let's start off with what nodejs is and
this is pretty much right off the bat
gonna sound like nonsense and like I
said I'll circle back on everything no
js' is a JavaScript runtime built on top
of Chrome's v8 engine it is scalable
event-driven has fast asynchronous IO
which is input/output it's generally
used to make web servers and developer
tools so first thing right off the bat
is talking about v8 if you have no
before you've probably not really heard
much about V a v8 is the JavaScript
engine inside of chrome that is used to
execute execute the JavaScript you have
on your website so if you ever have
built a website with JavaScript whether
using just plain JavaScript using
angular react Jas anything like that
when you run that application inside of
a web browser the part that actually
executes the JavaScript is v8 if you're
using Chrome the cool thing about v8 is
it's not actually tied to chrome itself
it can be embedded into any C++
application it's cross-platform
so it runs on basically any operating
system
you would expect a user to have Windows
Mac OS which be Chen's BSD stuff like
that the C++ application that it is used
in that we're talking about is note so
it is basically VA outside of the
browser and embedded into a separate
runtime for JavaScript on top of that
note provides a lot of libraries that
aren't initially available in v8 and
we'll run through a couple of those so
I'm going to go through some quick
examples to show exactly what that means
up until this point what questions you
guys have anything all right
first example we're just gonna do a
simple hello world application if you
guys have ever programmed anything I've
sure you seen this before but this is
just who all in here doesn't have
experience in JavaScript before I get
started in this they didn't have to be a
lot of experience just like any
experience okay good so I won't explain
what console.log is but
and right there it's printed out hello
world so how we ran this application is
we just call the node executable and
told out what script we wanted to run
and then it'll run through that script
and without a browser it will execute
all the JavaScript in it I know this is
a super exciting example but it goes to
show just the ability to run JavaScript
without at web browser so we'll go to a
little bit more in-depth example that
show some stuff that you couldn't
necessarily do in Chrome
and I'll go ahead and run through what
this code does before I run it the first
thing it does is it prints hello world
for you guys again
then it accesses a global variable
that's a part of know called process
which is something that's note specific
and you don't you don't really do in the
browser and it's a lot of metadata about
what application you're running and a
lot of global thing you're gonna paddle
things to it so if you have variables
that you set outside of the application
or something like that and we're
accessing the arguments on it so
basically I want to call this
application and pass arguments and then
it's going to iterate through all those
arguments and print out what the values
of them all are and then it'll print
we're done when we're done
so what it's done is it rated through
all those arguments it actually took the
executable of no that's the first one
it took the script that we gave it as a
second one and then all the things that
I passed it from there on this shows
that we can basically use things from
our operating system that aren't that
don't have to do with our browser so I
can pass it anything I want to a lot of
people will use us to do environment
variables so if you have things like
secrets that you don't want to actually
store in your code passwords to services
database keys stuff like that they'll
get passed in and sort of a similar
fashion depending on how you set up your
application what questions do you guys
have about those before I go back to the
slides well so you you can pass a third
argument to 4h or it returns a third
argument that is the actual array itself
and if you wanted to you could check the
length of that array but what 4-h does
is it iterates over the array and it
stops up the last element of it so you
won't go over however many there are so
if it takes all those arguments as an
array and goes through each single one
of those and performs this bit on each
single one of those so you're not gonna
go out of the bounds of that array or
anything because of that is that kind of
answer what you looking for okay
you can write developer tools with
nodejs and there are lots of those we'll
cover probably the biggest one a little
bit in a little bit called NPM but
beyond those notice primarily used to
write web servers web servers for those
of you that aren't familiar are
basically how content is stored and
served on the Internet it's where users
data tweets posts and all their
information are stored so kind of an
example of this is when you're say
you're on Twitter and you tweet
something when you when you hit tweet it
doesn't just stay on your computer it
has to go somewhere that way when other
people who are using Twitter and are
following you on Twitter want to see
your tweets they can get them from
somewhere so you send to it you hit
tweet it sends it up to a web server in
this case the Twitter's web servers and
the information is stored there for
anybody later to come back and request
so we're gonna kind of cover what
something called the client-server model
is so I'm gonna go in through a couple
examples of that two web servers
communicate with databases with file
systems they communicate with other web
servers so if you have if you've ever
heard of like sequel server or my sequel
or anything like that they're running
they're not running on your computer
when you're running web applications are
running on a web server somewhere say
you have a for instance say you have a
tweet that has a picture in it I don't
know how exactly Twitter handles our
images but how you could handle it is
you could save it locally on to that web
server which you can't which means that
like anybody who makes a request to that
web server that web server will then
access the file system on the web server
instant that image down so it has a
centralized place for all of your tweets
all the images you send up basically any
information that is stored on Twitter is
on their web server it can also web
servers also communicates to other web
servers through networking protocols
like HTTP which is mostly what we'll
discuss today an example of that would
be you send a tweet that has a link to a
free code camp blog post and when you
see that tweet on Twitter you'll see
like usually see like a little image
that has to do with it the link a couple
sentences about the link what happens is
you seen that sweet it goes from your
browser to a web server to Twitter's web
servers then Twitter's web servers see
there's a link in it and then they make
a network request to free code camps web
servers
so now that happens on your computer it
all happens on their server web web
servers can be written to know and if
they can be written in Java Ruby c-sharp
and a host of other languages today
we're gonna be focusing on node what I
just described to you guys though is has
a specific name it's called the
client-server model and it's a way to
split responsibility between your web
server and the users browser where the
web server handles databases inter
client communication and authentication
the client handles view logic requests
info from other servers stores data in a
database for instance with something
like Facebook if you are on facebook
messenger you send a message to somebody
on Facebook it wouldn't go directly to
their phone it will go to Facebook
servers and then their phone would
request that or it would push it down to
their phone depending on how they set
that up do you does anybody have any
questions about web servers are kind of
what the client-server model is I can
show you after this I'm going to show
you some code of like what a web server
like looks like no questions all right
before we get into that actually I'm
gonna talk about why you would and
wouldn't use no Jass and once again this
is gonna be nonsense since I kind of
break it down and explain it step by
step but no js' is good for asynchronous
not a sink into the applications that
require non-blocking input/output and
our event driven doesn't mean much I'll
show you examples no Jess is not good
for CPU intensive tasks so anything that
requires a lot of math machine learning
AI stuff like that note isn't very good
at that because it doesn't have a way to
handle it in a non-blocking way and it's
also good for non server applications
it's also not good for non server
applications like game engines mobile
applications stuff like that so like I
mentioned earlier when I mentioned non
blocking events and asynchronous i/o
what that means is so say you had a web
server that read a file from the file
system and sent it back down to a
clients browser who was requesting at
that say it was an image sometimes
reading files from file server takes
time so you could have a very slow disk
and it could take a couple seconds so
person a with
web browser makes the request for that
image person be with another web browser
then makes a request to the same web
server but they request something and
that's a lot quicker
for instance they want to get the
information about a user they want to
look at their profile on Twitter and for
some reason that takes longer than
reading that image from the disk does
for person a so person a makes the
request and because the input output and
note is non-blocking it basically pushes
that off to the background by itself and
continues that that disk read while
person B can freely make a request for
that user information and pull it back
down and it gets pushed back down to
their computer just fine then when
person a when the image is done loading
from the disk it gets sent back down to
person a well what happened in something
that was not was not non-blocking her
was blocking is when person a made the
request for that image the server would
stop everything until that image was
read in from the disk in person B would
be stuck waiting to see their profile
information even though that's a lot
quicker than what person a is trying to
do so they'd have to wait for person a
to get done before they could do
anything in that isn't good because
these things are running on your servers
and their clients on completely separate
computers completely different parts of
the world you don't want them to have to
think oh this is going slow because
person over here is doing something else
like that that's not gonna not gonna fly
with them really do we have any
questions about non-blocking i/o or when
you would or wouldn't want to use node
was an example of something that would
be secret Ness okay the question was
what's an example of something that
would be synchronous versus what I just
mentioned that something that was
asynchronous so reading that image from
your disk on your computer could be
synchronous but no does it in an
asynchronous way so what that what I
mean by that is when you when you make
that request for that image
like I said node pushes it off into the
background it continues doing other
things if it was synchronous and you
could read that you can note you can
actually read it you can read stuff from
the file system in a synchronous way but
a lot of but it's like default
asynchronous if it was synchronous it
would stop everything else down the
chain from happening so all their users
so the things that are asynchronous
could also be synchronous it just
depends on what you what you want it to
do and with no the synchronous stuff is
kind of the default so anything that is
asynchronous could also be synchronous
what other questions do we have all
right I have more code examples these
are actual web servers this time
so the first one is going to listen for
I'm going to show you two applications
they're both pretty similar the second
one's just a little more in-depth the
first one just listens for and events on
the server the second one listens for
specific types of events and does
specific things for those alright so I
will run through this code first the
first line of this code is requiring in
the HTTP module bits of code in node
that are provided by something other
than your applicator than the script
you're in or called modules so you can
write a module and store it in your
application node provides a set of built
in modules HTTP being one of them there
are third-party modules that you can
install through tools that we'll discuss
later called in a tool we will discuss
later called NPM but this is how you
require them and it basically gives you
access to everything that's in and
exported from this HTTP module in the
variable that I call HTTP so the first
thing we'll do is create a new server
HTTP has a function on it called create
server and you give that function a
callback
and what that means is anytime anybody
is make makes requests to the server
this highlighted code will execute and
I'll show you in a second what this
highlighted code actually does we assign
that server to the variable server
because we do stuff with it down here
here we are creating a port and I'm
going to be running this this locally
with nothing else on my computer so the
example supports won't you have to
define a port but a port it doesn't
matter that much in this situation but
what a port is is it's basically a way
to tell specifically what you're talking
to on a computer so there's a bunch of
there's a bunch of networking ports open
on my computer right now for different
reasons say I had five node applications
running on my computer they can't all
run on the same port so one one around
on eight thousand one on a run on eight
thousand and one one eight thousand two
and so on and you make requests to that
specific port that way when you make a
request it's you know what what you're
talking to and the node applications
know which which of your services needs
to respond
so the server variable I defined up here
when I created that server I'm calling
it the dot listen function on it and
what that tells you is it tells the
server to begin listening for network
requests against it I passed the port
and then I pass it a callback so this
code is executed when the server begins
listening so the first thing they should
run when I run this is this console log
right here saying the server started to
listen on port and then the port number
so let's go ahead and run it we're gonna
write in the same way no and then the
name of the file which is event server
about yes and there we go the first
thing I did it says server started on
port 8000 and then nothing the
application didn't finish it's still
just sitting there open well what's
going on what it's doing is it's waiting
for me to make Network requests to the
server and when I do make network
requests this bit of code is going to be
executed so let's go ahead and make a
network request to do this I'm going to
use a tool called postman everybody kind
of see that light theme okay
is that better
I don't you can't I haven't seen a way
to zoom in postman I will very
explicitly talk about what everything is
happening it so I'm gonna type localhost
localhost it is my computer so this is
where you would type the like the
website you're talking to like
google.com or whatever and just
localhost is my machine and then the
port so I know what application to talk
to I'm making a get request which is an
HTTP method we'll talk a little bit
about HTTP as we go through these
examples get is one of the requests you
can make through HTTP which is a
networking protocol there's a bunch of
other ones you don't use most of them
most of the time use a couple of them
and I'm going to go ahead and send that
request the response is a JSON object
which if you're familiar with JavaScript
you're probably familiar JSON but does
anybody not know know know what JSON
JSON is and would like me to go into
that awesome
so what JSON is is a way to basically
collect and organize data so say you
have a bunch of information that you
want to send around between applications
or send to a user you would send it in
Jason's a good form to send that in JSON
has a series of keys and values and the
values can be more JSON so the key in
this instance is message and the value
is hello world you can have as many keys
and values as you want so if you were
making a request for like your profile
on Twitter it would have your username
and then the value would be your
username and then it would have your
profile picture and the value would be
where you get your profile picture from
and stuff like that
just status I'll go to the status in a
second I'll show you the code first so
what happened when we made the request
of this server this like I said this
code is what's being executed anytime a
request is made in a node application
you Jenner you're gonna have access to
two variables request in response the
request is to the data that the client
sends you and their responses the data
you're sending back to the client the
first line of code we're taking this
response and calling a function called
right head and what that does is it
writes essentially metadata to the
content that you're sitting back to the
client I'm sitting back at 200 which is
an HTTP status code 200 is the universal
way in HTTP to say that went great
everything did exactly what it's
supposed to do here's your information
and I'm writing a content type of
application JSON and all this is doing
is it's telling postman or the browser
or the users mobile phone or whoever is
making this request that this is the
type of data I'm sending back I'm
sending back JSON then I'm writing the
actual data I'm sending back to the
response called by calling response not
right I am call I'm sending back that
message helloworld back down to postman
and then I'm calling response not end
which sends the response back down to
the client so we'll go back to postman
and I'll show you where all of these
things are showing up at so this is the
data that got sent back that I explained
earlier the message helloworld this is
that application JSON that I showed you
being sent back the rest of it is stuff
that note is adding itself and here's
where it shows that 200 okay there's
lots of HTTP status codes you've
probably seen 404 before means the
resource isn't found you might have seen
500 it means their server their server
threw up basically I mean something
happened on their end and you just need
to keep going about your business what
questions do you guys have about this
little web server
anything specific all right so I'm going
to show you the more in-depth example
now
so that server what it did was it listen
for any requests and I responded to it
the exact same way
this one will handle different requests
a little bit differently so here's the
main file in the application it probably
looks pretty familiar except I require
in an additional file so the first thing
I require in is HTTP which is nodes HTTP
module that we've discussed a little bit
the second thing I require in is a file
called a router router is a file that is
actually inside my application so it's
something that I've written made a
module and then I am exporting to be
imported elsewhere that router file
could be used in thousands of other
files but we're only really gonna be
using it for this one same thing here
I'm calling HTTP dot create server it's
doing something a little bit different
on the requests but I'll go into that in
a second port 8000 same deal and I'm
calling server dot listen and I'm saying
the server started and is listening on
port 8000 so let's go ahead and start it
up starting it the same way node and
then the file name server started on
port 8000 so what is this doing
different this on a request is calling
my router function and giving it a
variable that is on the request that the
user sends in so I spoke a little bit
earlier about this get up here
there's also post put delete it's taking
this and it's performing a different
action based on what kind of HTTP
request is being made and then I'm
passing that function that's being
called their request in the response so
it can perform operations on it the over
to my router file the only two things
that I'm actually going to perform
anything on just for sake of brevity is
a get request in a post request so to
explain the difference a little bit a
get request is is a client being phone
web browser mobile app saying can I get
this information a post request is a
little bit different a post request is
you as a client saying go ahead and
create this information on the store
so back to the Twitter example when you
tweet something and you you click tweet
or you click send it posts it to the
server and it says hey here's a tweet
save this stuff or when you new users
created in your application using hey
here's a new user save this stuff and
that's what a post request is so let's
go ahead and do the get the get request
I'm gonna do it a little bit different
this time just to show some different
examples I'm going to add something to
it called a query parameter so at the
end of the URL I'm going to add some
data I'm gonna add name I'm gonna say
name is Logan because that's my name
this could be any information but it's
the it's it's it's going to it's there
basically to narrow down the stuff you
get back we're not going into all that
logic right now I just kind of want to
show you an example of there being more
information in the URL on the server
handling that information responding
that information in a specific way so
I'm going to go ahead and send it and
here we've got a JSON response back in
the same way it says message thank you
for getting its a different message than
it was last time because it's a
different server doing different logic
and it returns the query arguments I
sent name equals Logan normally you
wouldn't just have a server that sent
back the data that the client sent up to
it that doesn't make a lot of sense I
just kind of want to show how the
plumbing of node works and how a web
server has access to this information
and what you said all is and once again
status is 200 what questions do you guys
have up into this point
all right don't hesitate to ask
questions guys I like questions
questions are good because if you're not
asking questions that makes me think I
said everything perfect and explained
everything perfect and I know that's not
true all right so here's the get request
because we are over here router not
method method is going to be get so it's
going to execute this code it's being
passed the requests in response the only
thing we're doing a little bit
differently here is we're taking we're
trying to discover what that those query
values are we're using a module called
URL which is a built in module in the
node that's being required up here oh
cool using a laptop mouse stuff gets
dragged around sometimes
so URL has a method on it called parse
you pass parse the URL you want to parse
and what parse does is it breaks apart
the URL into usable chunks of
information so you don't have to take
that big long string of data and and
take it and find the information you
want on it this will give it to you in a
way that's a lot easier to consume and a
lot easier to handle requests has a
method on it or has a property on the
called URL
remember that request is what the client
is sending you the web server so it
takes this URL parses it and then
there's a parameter on it called query
being those query arguments and I'm
getting that parameter off and assigning
it to query and this is gonna be a
little bit more of the same response all
right head I'm writing a 200 saying
everything was successful and that app
with that JSON is the type of data I'm
sending back to the client and then I'm
writing the message which I'm saying
thanks for getting and then I'm putting
that query back in that thing that I'm
sending back down to the client I'm
calling end which sends it back down
what's up the question is is the mod is
the module that I'm exporting right here
a dictionary of functions and that's
exactly what it is it's basically a
dictionary or if you use other languages
or called associated arrays dictionaries
are using what they're called in Python
and a couple other languages because
JavaScript has was called first-class
functions they've values and these
dictionaries are functions so when you
when you access things in addition air
you can pass it stuff and then perform
the logic on it I guess I let's cut back
a little bit this like I said this is a
module modules dot module to exports is
how you export the code as a module to
be consumed by other stuff so how wanted
information to not be available to
anybody else but I wanted some of it to
be available I could do code here and
it's not being exported it's one of the
few ways that JavaScript offers
encapsulation if anybody knows what that
is so now we are going to talk about the
post request which is a little bit more
complicated luckily you probably won't
ever actually do this there's a lot of
there's a lot of node libraries that's
that abstract this for you so you don't
have to get down and dirty reading this
data in like this but I'm going to kind
of show you what it looks like and if
you don't understand how some of the
data is being read in it's not a huge
deal you like I said you're not gonna do
this right off the bat so I want to go
ahead and make that post
I've actually got alright here so I
don't have to type it all out
so I've got it set to post here which
means hay which means the client saying
hey server go ahead and create this data
and save it however you need to save it
I'm telling the server right here in a
header that the content type is gonna be
a basically a form this is the content
type you'll usually use if you're
filling out a form on a website
remember this content type header is the
same thing that the server used to tell
the client that they're sending back
down JSON so you're telling it you're
sending you're sending up this form and
it will probably send back down JSON
this isn't gonna send down to a slum but
the other examples did and then I'm
sending it a body and what that body is
is the data that I wanted to create so I
gave it a name and I gave it an address
the server could require any any
combination of information in this body
but this is just for the example what
what we're sending name being Logan an
address being one two three no Delaine
any questions about this post I'm about
to make to the server all right it is it
is not in JSON
you can't send data up in JSON how I
don't do a ton of front-end work how I
think the form URL encoded content type
works is I think it's gonna append it to
the URL but you could send a post JSON
you can set up was XML you could send up
a bunch of different stuff I'm sorry if
that information was wrong so we've got
the post right here so how this data is
gonna come in is it's gonna come in and
chumps basically so as the client is
making the HTTP HTTP request to your
server it's coming in not as one big
chunk of data over the network that
would make a lot of sense would not make
a lot of sense because it could be a
huge request it's coming in a little
tiny bits so it's coming in Part A Part
B Part C and then will let my server
know in the last chunk of data that it's
the last one so I am creating an array
called body and this is where I'm going
to be putting the data that's being sent
up to the server
here is where I'm registering events on
the request object what this is saying
is anytime this event happens on this
request I mean have this thing happen
execute the code that's given so the
first one is data the data event is when
one of those chunks arrives from the
client to the server so this will happen
could be hundreds of times depending on
how big the request is and this is end
this is saying execute this code when
the last chunk of data has arrived to
the server so all we're doing when this
chunks come in is we are pushing them
into the body array now this data in the
body array isn't exactly usable for us
yet because of the way the HTTP request
comes in but we will clean this data up
and make it make it usable for our
purposes so here's where things get a
little bit hairy and if you don't don't
get all of this it's not it's not a huge
deal because you probably won't actually
do this this is just I want to show that
note is kind of a JavaScript so
higher-level language but node provides
some lower level doesn't provide
high-level abstractions as you might be
used to there's stuff built on top of
node that provides those distractions
but no one has some kind of lower level
stuff in it
so here's query string I required it up
here it's another module note provides
and I'm telling it to parse this data so
what that array is from the data that's
coming from the client it's called a
buffer which is the type of data or type
of a datatype basically so basically I'm
telling the buffer to do is take that
big array concatenate it into one long
buffer and then change it to a string
there's lots of good information on
nodes in notes documentation about what
a buffer is and how buffers work with
HTTP requests this kind of outside the
scope of this but if you want to get
into that it's pretty interesting so
that's gonna be the kind of data that
query should enforce when it's all
string so it puts it tooks that big
buffer makes it one long buffer and
makes up buffer into a string and then
the query string library parses it and
then I signed that to a variable called
parsed post data all right down here
because generally when you post up to a
date this post up to a server is gonna
get saved in a database of some sort I
made a little a little file called
repository it doesn't actually do much
because I don't need to set up a
database to make this example but we're
gonna pretend that be save but the safe
function here saves the data but right
now all it does is say that it's been
saved to a database in the real world
this module could there could be errors
for instance if you're saving something
to a database and you don't have you
don't have you can't make a connection
to that database for some reason or the
database is down or there's something
wrong with the data this could throw
what's called an error we're gonna this
won't actually throw an error cuz I
don't have a set up to but we're gonna
pretend it could which is one of the
good thing about modules modules in this
situation allow you to not know what's
going on inside of a module but still
call the stuff in the module so I don't
know that this isn't throwing an error I
just know that is that it should be
saving the data so if there was an error
I need to wrap it in what's called a
try-catch block try it means X you all
of the code in the try and if the code
in a try has an error
stop executing it and perform what's in
the catch this code will never get never
get accessed but if it did here's how it
works I said when I create a variable
called response code I want to send back
a 201 down to the client the save is
successful what a 201 is like a 200 it's
basically saying everything is ok but in
the constants of creating data in the
context of creating data it basically
just means data's been created or if
there's an error I want to log it out
and I want to say 500 there's been a
server here so as calls repository about
save and passes at the data because that
thing doesn't actually do anything it
succeeds and I set the response code to
200 it writes the response code to the
response and then it ends a request
which means it sends it back down to the
client so let's go ahead and do this
and here we go response code is 201
created and I don't send any data back
because there's no reason to really with
a post you're just creating the data you
don't need the data back you've already
got the data all you need to know is if
it was successful or if it wasn't and
down here we said we saved this data to
the database all right what questions do
you guys have about these no questions
all right so let's talk a little bit
about NPM once again this give me all
the situations where I say bunch of
nonsense and then I circle back to what
each of them mean when I'm done
NPM is a package manager in a registry
for node NPM is also a CLI tool to
manage the packages on your machine
which is that package manager it allows
users and enterprises to share and use
over 600,000 packages so there's a lot
of code on NPM that's not yours you have
access to a lot of it not all that's
great a lot of it's very very very very
good and pretty much indispensable to
the node ecosystem so let's talk about
what a package manager and a registry
are a package manager is a way of
managing code your project depends on it
makes it easy to incorporate new code
into a project and helps maintain
consistency between installations of
your project so say you have a
dependency on a library that somebody
else wrote a package manager makes it
easy to incorporate that library and
bring that library into your project it
creates a way for other people who are
using the project to know what libraries
your code is using so that way when
you're developing something and then
your friend takes over and starts
developing it or your coworker takes
over and starts developing it they have
a way to reinstall all those packages
that you are using when your code is
done and you're ready to deploy it the
server knows where to get all those
packages from and all that is because of
NPM a package registry makes it trivial
to share the code you've written and
easy to access other's code others have
shared so the package manager lets you
handle those packages on your computer
download them install them everything
package registry is a place where you
can upload code for other people to use
so you use the manager to access the
registry basically does anybody have any
questions about NPM I'm gonna go into
some examples of how this all works and
set up a project using NPM in just a
moment all right
so we're gonna create a new directory
here and call it test NPM
I did not create a very place
so how you create a new project and say
this is a project that's gonna be that's
gonna use NPM either to be uploaded to
NPM website later for other people to
use or for you to download other
people's packages to use on your
application you type in NPM and Nitz and
this is an example of a developer tool
that was written a note so it's gonna
ask me a couple questions for how I want
to set up the code how I want to set up
the project and then I'll show you where
these settings are stored it's gonna ask
me the package name I'm gonna leave it
default it's called test NPM version 1.0
description a little test
NPM thing the entry point is the main
application the main domain file in your
application I'm gonna create an index
js4 this a test command this is if you
have automated software tests we won't
for this example a git repository
there's none for this but if there was
you put the github URL here if you're
familiar with github keywords this is
where you could type it this is like a
library that handled managing dates or
something managing like date time
strings you put dates here but this
isn't gonna do anything so we're gonna
put anything here either author Huskins
license is gonna be in my see cuz I
don't care what anybody does with it oh
that's okay
so what this did was it created a file
called package JSON this basically has
all the settings that I just specified
name version description the main file
scripts which we will go into in depth
author and then the license we actually
don't have a test script for this so
we're going to make another common
script you'll see which is start and I'm
gonna call node index is remember
earlier how I whenever I ran the project
I was typing in node and then the name
of the file now I'll be able to type in
NPM start and it will do that for me so
let's do a new file call it index ajs
and I'll show you why that's useful
so we're gonna go ahead and run NPM
start and I ran hello just as we'd
expect by writing hello and a console so
why this is useful is I want to take
that longer example with all the
command-line arguments and copy it into
this file and remember how before I had
to type in node and in the name of the
file and then typed hello and then my
name and then free code camp I can do
all that in my scripts so now when I run
it it executes all of them just like we
had before but I don't have to type in
everything every single time I do this
is useful because this make this part
when you have a big production
application can get really complicated
you can have scripts that run before
your application starts up you can make
sure your database is in a good state
you can have lots of variables that your
application needs that are provided from
here a lot you can do lots of stuff you
can also have scripts that generate
documentation for your application
scripts that run database migrations
anything of that nature so now I'm going
to show an example of us installing a
package through NPM then we'll wrap it
up and I'll take questions
so to install a package from NPM you can
actually let me show you in PM's website
where all the packages are so this is
the NPM website and there are 600,000
packages give or take on here a popular
example is a package called low - low -
provides a lot of little utilities for
you to use in your code it's kind of a
generic set of functions that do stuff
that you don't want to have to implement
yourself because they can be kind of
complicated and miserable so we're going
to install low - in our application how
we're gonna do that this example is it
they have an example over right here for
instance if you uh see how popular this
stuff is how many people use it low -
has been downloaded almost 15 million
times this week that's a lot that's just
this week I'm probably responsible for
five or six thousand of those
we run a lot of continuous integration
that might not have been an exaggeration
it might actually be a couple thousand
so we're gonna salt npm install - - save
low - make sure you spell what you're
installing right because their stuff on
NPM that looks like other stuff so make
sure you're actually looking at what
you're installing when you install it so
you don't sell somebody else's code that
acts like low - but isn't actually low -
so we'll install it it's gonna contact
NPM registry to get that get that data
look we now have a dependency list and
it tells us low - and what version of
low - we're using so now in our
application I'm not going to show you
how to use low - for what low - is but
it's just a popular example so here
we've required it in like we have
everything else everything else that we
had from the node standard library stuff
the stuff that I've written myself a lot
of people will name the variable low -
and underscore you can name this
anything you can name is low - or
whatever else you wanted to but most
people just call it underscore so if you
ever seen anybody using underscore dot
something in JavaScript they're either
using low - or underscore probably to do
stuff and there's like I said there's
there's tons of other packages on NPM
that abstract away a lot of the nasty
stuff that I was doing in that post
request and one popular one is called
Express I use Express every single day
it's got about 5 million downloads on
NPM this week Express makes it super
easy to do those gits and those posts
without having to get down and dirty
with that data all right what questions
you guys have about NPM
yes yes so what the question was was in
my package.json I have a script I'll
start can I have more scripts here yes
you can have tons of scripts here
for instance we could have a script
called doc and what that does is
generates Doc's
you got your documentation for your API
isn't it actually gonna do that because
generate doc doesn't do anything but you
could and for that you would type in NPM
doc and it would pretend it does
something it would generate your
documentation for you you can do
probably the most popular one you'll see
other than this is test and if you had
automated software tests you would type
an NPM test and it would do all the
things to orchestrate those software
tests for you so you can put as many
scripts there as you want these scripts
can be so this this stuff can be other
files so you can have a file you can
have a directory in your project called
scripts that does a bunch of really
complicated stuff and just call it with
NPM test or whatever that's pretty much
what NPM test does can I run multiple
scripts at once
yeah there's let me I'll just do the
same thing twice I'll run it once with
all the arguments and ones one once
without though you can the way I would
do this though is I would do a make a
script called start and test and do note
or do n pm start MPM test it would run
both of them for you or you could do
that from your command line but if it's
something that you did frequently you
could make a script for it which is the
good thing about NPM is anything you do
a lot you can do through it in script
all right are there any other questions
one more question
come on guys
your question
Am I am I responsible for any of the
downloads of it he asked why am i if I
am responsible for any of the downloads
of the Express thing yes for every
download of lodash I probably also
downloaded Express the same amount of
times so all of the projects we run in
production at love's use both lodash and
Express so anytime we we publish those
which is it could be a hundred times a
day
those get downloaded so we do it all the
time and having NPM do that automated
for us is great I'm not responsible he
asked if I was responsible for all of
those weekly things I'm not responsible
for all of them but I'm responsible for
a non-trivial amount of them alright are
there any other questions before we wrap
up good question the question was do I
have any resources for how to learn more
about note or MPN this is knows website
they have on their documentation page
they have guides and there's a lots of
interesting guides and knowed a lot of
them aren't super beginner focused but
if you have a little bit of programming
background they're a great place to look
at it explains some more advanced
concepts of node like the event loop and
how buffers work and how blocking and
non-blocking and put output work free
code camp is a great resource I'm sure
they have tons of know stuff on there
I've seen some of it in there I've used
some of it personally and it's been
pretty solid and then there's tons of
just like people posting stuff on medium
that are like node articles and not all
of them are great quality but there are
at least a good place to start any other
questions
mmm yep they were supposed to eat - he
asked if they're supposed to be - Ann's
and an NPM script and the answer is yes
yes for all HTTP code specifically oh
yeah there actually are so a great the
question was are there any like cheat
sheets for the HTTP status codes that I
was discussing earlier and there are mdn
is Mozilla developer Network Mozilla
makes Firefox they have great
documentation for proven to anything
internet or JavaScript related they have
every single response code here you
won't use most of these you will use a
lot of them no it also has documentation
for their api's it's kind of hard to get
to go Docs and then you go to Doc's
again and then you click the first link
but here's all nodes documentation for
all their libraries for instance we use
the HTTP library here's all the stuff in
that HTTP library it's kind of hard to
read if you're not used to it that's
mine I wouldn't recommend just like
sitting down and reading this unless you
like have been developing stuff for a
while but it's there if you want it any
other questions before I wrap up all
right thanks everybody