So far in this section,
you learned how to require
Node JS core modules.
How to require your own modules.
And so in this video,
you're gonna learn how to require
third-party modules from the NPM registry.
Actually requiring a module
that we already installed from NPM
is really, really simple.
So again we do all the requiring
at the top and usually we require
the core modules first,
then the third-party modules,
and then our own coming from
our local file system like this.
So the one that we are going to require
is called slugify.
Remember?
This is the one that we installed
in the previous lecture.
And all we have to do to require it
is to just like before
use the require function
and then simply the name of the package.
And then OGS will automatically know
that it will have to go into
the node modules folder.
So this folder where all the dependencies are,
and search for that module there.
Okay?
So again this will then return a variable.
And so we need to save that into a variable.
And I'm calling it slugify here just like this.
Okay?
And so slugify will be a function,
which we can use to basically create slugs.
So again what is a slug?
Well slug is basically just the last part
of a URL that contains a unique string
that identifies the resource
that the website is displaying.
So for example,
in our node farm let's say
that where we opened this avocado
instead of having this ID equal zero
we could have something like
(clicking) fresh avocados, okay?
And so this part here
or actually just this part
so this would be the slug.
So it's basically this name here
and then based into a unique string like this.
And I'm sure that you have seen this
all over the place like in blog posts.
It's very common to do it like this.
Or really anywhere where we do not simply want
something like this.
Okay?
So ID equals one.
Or we could also just use
for example in this case here
goat and so on and so forth.
Okay.
I'm not gonna run a tab on the entire name.
But the idea is to use a slug,
which is this so a unique string
instead of just a number
which is not meaningful to us at all.
Now let's say this is the first time
that you're using the slugify package.
And so in order to figure out
how it actually works
we can search for the documentation
here on NPM.
So we just put the name of the package here.
Then search for it.
And so this is the one that we did install.
All right.
So we already did this part here.
So requiring the module or the package here.
And then down here we can see
how we used it.
So all we have to do
is to use slugify as a function
and then pass in a string.
Then down here you see the options.
And for example we can specify the replacement.
So basically what the empty space
will be replaced with.
And usually that's this dash.
So just like I was typing here.
But we can specify something different
like an underscore or nothing at all.
Okay.
Here we can specify that we want
everything in a lower case.
And so let's actually go ahead and do that
now in our code.
So we have slugify here
and now just to test it
let's do it here before we create the server.
Console at log.
Slugify to string.
Fresh avocados and then I'm gonna specify
the option that I just showed you,
which is lower case
and set it to true.
Give it a save
and remember that now no longer
we have to close the app
and restart it again.
Node man does that automatically for us now.
And so down here you already see fresh avocados.
I simply did some mistake here.
So it's lowercase.
But it's still not working for some reason
so let's go back here.
Oh I see.
It's actually just lower.
But that's exactly what
the documentation is for.
So for us to see how something really works
so that we can use the package
correctly in our code.
And so just like this,
I created a slug out of
our initial fresh avocado string.
Let's now just do that for all of our products.
So they are in data object.
So let's create an array of all the slugs.
So slugs.
So data object dot map.
And so this is again just regular JavaScript
so that shouldn't be nothing to you.
So we're looping over the data object
and then create a new array based on that.
And in each iteration what we're gonna do
is to take the element
and then create a slug out of that.
So slugify element dot product name.
So remember that is where
the name of product is.
So here you see product name.
And so then this object.
And let's actually log that one to the console.
And indeed here we go.
So these are our six slugs
or actually just five
out of our five products.
All right.
And so this is how we include
a third-party module.
Take a look at the documentation
and then use it actually in our code.
Okay.
So this slugify is now a dependency of our code.
Because without that package
this piece of the code here
would now no longer work.
Right?
So our code now depends on having
the slugify package installed in the project.
Now we could go ahead and actually store
these slugs here into the data json
and then build the app
so that instead of the ID
it displays the slug in the URL.
But I'm not gonna go ahead
and do that here because
that's just using the same concepts
that we learned before all over again.
And so that doesn't add nothing new.
All I wanted to do in this video
is really to show you this process
of integrating a new package into our code.
Taking a look at the documentation,
and then just do a simple example
with that new module.
So that was the goal of this video.
That is completed now.
And so let's now move on
to the next one.