Okay.
And up next, let me show you a few cool new features we can use in Node these days.
Now a disclaimer.
We will only use one of those things during this project, since the other ones, at least at this point,
are still in experimental phase.
But who knows?
Maybe by the time you're watching it, it's already stable and you'll find the use cases in your own
applications.
And technically, if you don't want to follow along, you don't have to.
And I'll try to make this somewhat short.
And essentially, there are three things I want to cover these days.
In Node.
We can actually use Fetch API.
And that's one of those funny things where when I was creating a node course in the intro, I specifically
say the difference between browser JavaScript and node JavaScript is the fact that we don't have fetch
API, but.
These days we can actually use right away fetch API in our node application.
Second one is Global Await or you will also hear this name top level await, which will actually use
in the application.
And that's something really cool where we don't have to set up a function just to use Await.
And there's also a watch mode which is a very useful alternative to node mode.
But during this project I will use the Node one simply because while I was developing, once you get
to the point where you need to run both of them together, I noticed that there are some glitches.
If you use the watch mode, which I'm about to show you.
So let's start with fetch.
And in order to save everyone's time, you know what?
I will copy and paste the URL.
Now, of course you can use your own URL.
You don't have to use this one, but I'm going to grab this code, go anywhere in the server.js again,
doesn't really matter where you run this code and what's really cool that right out of the gate we can
use fetch so it's provided to us.
We don't need to import anything.
We don't need to install anything right away.
Just like in a browser.
When we type JavaScript, we go with fetch, we provide the URL.
In this case I'm fetching from my own API.
Then it's a promise.
So effectively we just need to go with Dot.
Then we can of course turn this into Json and all that.
So let's write here RES.
We're going to go with arrays, dot Json, let's invoke it and then let's chain another then and let's
just log what we have.
So data and then we're going to go with log.
And data.
And if everything is correct, once we restart the server, basically Nodemon does that for us.
Check it out.
We have in the console an array, an array of items.
Again, it doesn't really matter what we get back.
It's just super useful that we don't need to do anything extra to start using fetch in our node applications.
And if you're not too familiar with Node, this wasn't the case for a long time, so you needed to do
some acrobatics for quite a while in order to get this basic functionality working.
So that's the first thing, fetch one.
And again, we're not going to use this in this application, but it's super cool.
Then the second one is the top level await where since I have this done, since I have promises over
here.
A cool way to handle them is setting up async await.
And again, for the longest time basically in order to run this code as async await, we need to create
a separate function.
So basically it would be like const get data.
This is going to be equal to our async.
We don't pass anything here and then we go with await Fetch and then await Json and blah blah blah blah
blah.
And then we need to invoke that get data.
However, that's not the case anymore.
So we can right away just start using await whenever there is a need.
And this is something we will do when we connect to the database.
So this is a cool feature.
We'll actually use it in the project.
So first let me just showcase the old way and then we'll restructure it to a new way.
So first we're going to go with some kind of response.
In this case, we're using Await.
Effectively.
We want to cut the first line over here.
Okay, awesome.
And then we want to grab that Json, Correct?
So same deal.
I'm going to call this cart data because this is what I'm getting back from this API.
But again, the naming here is totally irrelevant.
We go here with Json and then we'll just log the cart data.
Khanate.
Okay, awesome.
And then.
In order to.
Run this functionality.
Yes, we do need to invoke get data.
Otherwise it's not going to work.
So again, let's run it.
And if everything is correct, we should see the array in the console.
But these days we can nicely skip all of this and I'll right away set up, try and catch, because this
is how you usually want to handle async await operations because obviously you do want to handle the
errors as well.
And since I don't have to set up a separate function, I can simply take all of this logic out, set
it up in my try catch and just log the error.
And again, if everything is correct, we should see the card data in the console.
Super, super nifty.
We don't have to set up anymore a separate function just so we can implement async await.
And last one is the watch mode.
So we want to go to package Json and I'll stop my server over here.
And remember, we're using Node one to watch the server.
JS But at this point there's actually a built in way how we can watch the file.
So technically we don't even need to install the package.
And that built in way is to add hyphen, hyphen, watch and then whatever is the file name.
Now of course you can type it directly here in the console.
It's not against the law, but since we're setting up a command here in the script, it kind of makes
sense to do it the same way.
And we just need to come up with a name.
In my case, it's going to be watch.
Then we go with Node.
Then like I said, hyphen, hyphen, watch.
And then which file?
So in my case I'm going to go with server JS and then in the console we want to go with NPM run and
watch.
So let's save the package Json.
Let's run this command and essentially if everything is correct, once I remove the fetch functionality.
Node should restart the server for me, and if I take a look at my console, I can clearly see it's
the case again.
While I was building the project, I was using the watch, but the moment I needed to run the frontend
and the server at the same time, there were some clear glitches and therefore I'll stick with a Node
one.
Again, this is just to showcase that you have such option if you want to use it in your own projects.