Node.js


language: EN

                  WEBVTT
But I only have a button that says details and I do not know how to connect it
Should I connect it via my local terminal or should I use this this browser version of the desktop?
Can you
Share your screen maybe
I
Have a couple of tabs here like courses course desktop stand-alone recordings and
I have I have now on course desktops and it showed by virtual machine
But it only has a button eaters and the other is course that says no JS and I do not know
What should I click?
Can you share your screen, please?
Let me just
Okay, and I see Stefan and Melina don't have their desktop ready
Okay
Everyone that one else you can click on the the icon on the bottom left the
Like the little squares the nine little squares
Okay, click on no
So what he click on no GS course no, yes. Yes. Yes
And then click to unconnect. Yes
So if you click on the
Under icon on the bottom left
the little square the little
nine white squares
At the bottom and then you start
Typing code you should have
Visual Studio code icon and then you can click on it and then
Yes code ready
If you have that you can yes just stand stand STA and stand, okay
STA and and it should be good STA. Yeah, it looks it looks like it works. Should I open the terminal?
You can yes or you can stay in VS code as you wish
If you are in VS code, yeah, let's let's all just
Dive in and just use VS code
Did you all use the VS code?
Okay, I used it
Okay, so we are we are going to use VS code and it's really nice to code in various languages and
Especially in JavaScript. It was created. It is created in JavaScript actually type script
It it embeds a crow and
because it is
Based on electron, you know electron electron is the base of many of the
Applications you use like slack or discord or VS code or
I don't know what teams there are a lot of
applications that you use that were
that are built from
Electron meaning from Chrome and are actually web applications
Okay, so this is
This is VS code and if you
If you type control J in VS code control J, you should see the panel
right
the panel is the the bottom part of the VS code and
if you
Type again control J it will disappear
Okay, so this is a shortcut really important to access the panel
Okay
Yes, please please type Stan STAN
No, no, no, no, you don't it's already installed
Okay, and
so
so what I wanted you to know is how to
Close and open the the panel with control J. Okay, this is really important you use this shortcut a lot
so
you since the default tab in the panel when you open it is the terminal and
In the terminal you can type note and then enter and then you are in what we call the rebel of note
note rebel
rebel means
read evil print loop
So it's a loop where you can write something and then type enter and it will be read
you got evaluated and then
something will be printed and
then you get back to
Position when you can start writing code again
Okay
So everyone is there. Yes, I think so
No, Christie who's Christie I guess question yes, okay perfect and
Malina I
Don't see
so type control J
Yes Jaguar yes
Yes, so and then you type node and
Stefan also you can type node and enter and Vlad you are on your own machine, right?
Okay, perfect, where are you? Oh, yes. Yes. All right
Okay
The other thing that is great if you use the the the virtual machine is that I can see what you're doing
So, please do know that I see what you're doing
Please be advised so
Everyone is on the rebel
So now you can
Write
one plus one and
enter and you should see two and
Then you are back to a position when you can write some code. Okay, so now
you have
Wrote you have written the first JavaScript code in node.js and you should be proud
You can you can use some of the
of the code that you might know in JavaScript like
math dot random and then parents
If you see my screen
No
and then I can math with an capital M random and
Parents parenthesis
Okay, do you know this math random
Yes, exactly and you can use new date
And then you have the current date
Yes, you see here a T and then a Z at the end
Meaning that yes, it is the the UTC. Okay, and then you can
write
console dot log
That you may know
Hello world
Who cannot do what Stefan
Okay
Okay, can anyone guess why if I
If I enter console log hello world, I have hello word and then undefined
Exactly
console log
Returns undefined and that is why I have here undefined. I will talk more about console log
In just a minute
Hello world, so
Now you can
You can do that
So do you
Do you all have access to the course on
On your browser
Because you will need it to so you can copy paste some code will be much better
You know, I am sharing two screens right my VS code and
My browser and
Yes with the link provided in the chat, I hope you can all see it
Okay, I'm maybe you were not connected when I
Okay, okay
Okay, okay, okay my fault
Here you should all see it now
Yes
So in VS code you should all have here my media right on the top right in my case
Yes, yeah sure. Yeah. Yeah, perfect. Yes media API. Sorry, I
Call it my media, but in your yes media API. Okay
so why why that is is because I
opened
This directory
In VS code so in VS code you can type control K and then control O
Then you can open
Directory okay the folder and
The one that I
the one that I opened it with was
media API and
So if you close your VS code and then you reopen it
It will reopen with the same state that you left that in in
That you left it
Meaning that it will open with the same folder that it was open with when you closed it
Okay
So that is why there is media API
Here it is because you are in this
Directory this folder
Okay
now in this folder if you click on this icon the
Like the sheet with the plus on the bottom, right?
You can
You can write src slash index dot js and
VS code will understand that you want to create a folder named src with with within its
file named index dot js
Yes, I don't think that's an appropriate position but you can easily change it and
I will demonstrate why I
Don't like it. So if you do see Explorer on the on the top of the this is called the sidebar
Okay
Yes, yes, it's configuration that I did
so
This is called the sidebar. Okay, and with ctrl B
You can hide it and this is a shortcut that you must know ctrl B like bar
Toggle the sidebar
Okay
Next to it is the activity bar with all the the big icons
This is
The activity bar there is no shortcut to toggle it but you can add your own to toggle it I
do but and
On the sidebar if you click on the on the on the top
then you can have access to
To a
Menu and in the menu you can change the activity bar position
From default to top bottom no, sorry, sorry to
Yes, you can move primary sidebar left that way. Okay, and since I often use
control B to
To toggle the sidebar
Here what here's what happens?
Okay, you see I have to move my eyes from
Here
To the left when I try when I want to toggle off the sidebar
This annoys me. This is why I prefer it on the right side
If I do if I make it on the right side, I
Don't have to move my eyes of the here for you see that here word. Okay
So here I
Can write console dot log parents and the world
And there is much to say of this line what can you say about this time
Yes, it returns undefined perfect what else
What is console? Yes, it is an object perfect. How do you know it is an object?
Because after that there is a dot yes, what is the dot?
Exactly it is it is the accessor
Operator yes, and so it's
Allows
the the programmer to have access to any of the
Properties of the object which is on the left. So console is an object
Doesn't know is an accessor and log is what?
Yes, it is but before you say it is a method it is a
Property of yes, it is a property of console. How do you know it is it is a method?
Yes, exactly because you call it or you invoke it okay
So log is a function actually and it is called here as a method
What what in the hell did I say so I
Won't talk to you into the details but
You must know that in JavaScript there are no methods
There are only functions and any function can be called as a function or as a method or as a constructor
Okay
So
Log is
A property of console and it is a function and what is hello world
Yes, it is a string. How do you know it is a string because it's delimited with quotes single quotes
Okay, and you can you can create strings in other ways in JavaScript, what are they?
Yes
Exactly, what are literal templates?
Everyone is familiar with literal template because we are going to use it. Yes back quote. Yes or back tick. Yes
Okay, everyone is aware of that and
Understands what Vlad said
Don't be afraid to say no
Okay, perfect
so
Where does console come from?
It is yes in the browser it is a web API in node it's an environment API
So console is not in the specification. It is not
Specified in ECMA script, okay
So
Console is is not a native JavaScript object it is a global variable
Provided by the environment here by node.js. And yes, it is
Also available in the browsers but in the browsers the implementation will be different why because it's not standardized
It is not specified in any
specification
Okay, so you won't have the same
API in console in no GS and the browsers
Okay, even in Chrome
so
The dot is a property accessor and you know another property accessor
Yes
so this can be
So this is the same almost the same this is much more performant, please don't use this if
If this is static, I mean this is if this inside the square brackets are not
Is not a variable, please don't use it
okay, and
last thing to know about
Yes, I think you all
Um, I think you own
Yes, well said okay, everything is okay. Oh
Yes, you said hello world is a string but I wanted you I wanted
And at least one of you to tell me that it was an argument of the log
function
Okay, this is an argument and you know how many parameters console log accepts?
Yes, many it's infinite you can you can you can see hello world
Maybe yes, but you you won't I hope you can't you won't reach the limit
So now you can
exit
No DS using control C
In your terminal, so everyone is out
Okay, perfect and
Everyone has a console log in index.js, right?
Yes, yes, yes, okay, so now you can
Type on the terminal node
SRC dot slash index.js and then enter okay run
So any please type control C
Twice
Yes
Okay
Control C is the break for any command in the terminal in any chef in power shell
Okay. So now you can type node. Yes, everyone did except Melina maybe
All you
Stefan you did not
save
Your index dot JS
So you can type control s to save
Yes, and then
Yes, okay better. Okay, so everyone has
written
Maybe not Christie. Oh Christie Christian you did not
You I did not understand you were trying to
Okay, so please start please
Create a
JavaScript file in the folder s RC called index.js. So in the Explorer
You can yes, you can click on the
Gag on with the sheet and the plus
Yes, and then us RC slash index.js
Who is talking and yes, so yes
Yes, tell me
Yes, exactly in the in the rebel you saw undefined in gray not in white
right, so
It is it was just to advise you that your
Your function returns undefined, but you don't need to know
in
You don't need to know that your
Function returns undefined in your file in text.js
So it assumes that you don't need it and it doesn't show
So now
Now a bit of theory I hope I want I
Hope it won't be too complicated
But I think it's really important because one of you mentioned even the the event loop I think it was blood
And this is a really important concept
So
JavaScript the code that that you write in JavaScript
Will run in a single threaded environment. Do you understand what it means?
Okay, so
if the there is only one thread and it's called the main thread and if the main thread is
Occupied is busy. It cannot
It cannot do anything else, okay
so
This is good because it avoid complications associate with multi-threaded programming and if you are
if you have
Developed in Java or sorry
In Java or any other languages that are multi-threaded
Might have encountered
Difficulties about how you man handle
multiple threats
so
The JavaScript is
The code that you write is only executed in one thread but
Exactly exactly. Thank you for the clarification. That is well was it and
To
Despite that no JS can handle
multi multiple
Things at the same time almost at the same time because it's even based
Because it has an even base concurrency model meaning that
Everything is
Happening in an event loop in the event loop is an infinite loop
looking for something to do and
If there is nothing else to do the program stops, okay, um, so
The event loop allows for non blocking operations and
What it means in that is that some of your
code may access the network or the database or the the
file system and
Using the while it reads
The what the file system is occupied
Getting the file that you want to read for example
No, it can do
Something else and it will
that is the whole point of the even base and
non-blocking operation system
So
I'm gonna give you an example
So I said that your code will only be
Handled by one threat but in no JS
Under the hood there is a thread pool
Which is handled by the lib UV which is a library in
C or in C++ C++ or both. I don't remember and
So the thread pool
Thanks to lib UV means that you can call
At the same time several
you can try to read several files of non-file system and
When they are already all the files are ready to be read
the it will give the power back to
No, JS and your code and then you will be able to
Do some stuff with the files that you wanted to be to be read
Okay
No, it's it uses the
Thread pool of lib UV for either the file system database or network
Requests and
It's it's perfectly perfectly
Transparent for you. You won't you won't use the UV yourself
Okay, it just so you know that even though your code will only be executed in one thread
That doesn't mean that there is only one thread in no JS. It's more complicated. I won't talk about way workers
I
Will talk about
blocking and non-blocking
So blocking operations
Means that you won't be able to do anything else okay until the operation finishes
for example if you read
synchronously a file from disk
then
if it's a
pretty pretty heavy file
It will block the main thread for a long time
Okay, and the program won't be able to do anything else
with non blocking operations meaning that if you
Read a file asynchronously
That means that you command
note you command the file system to read a file and
You and you tell the UV that when the file is ready
Do do the execute this function, okay and
This is managed through callbacks or maybe promises
Okay, a single weight is based on promises
I hope you all know what promises are because it's a really complicated subject and it might take one hour or two
To to get the grip of
Promises so I hope you all know about promises. It's really important
Especially now
Everything is promisified everything even the the native API of node.js
Is now promisified
It was
It was once built
with the callback
System and now it's all promised
So
The system initiate the operation then moves to other tasks then
that you can
Know JS can do and then when the
The operation completes its executes the the callback functions
Okay
So this is blocking and non blocking operations
Now the event loop
The event loop is really really important
so
it's a fundamental part of node.js and
it's allowed
to perform non blocking I
O meaning input output operations like faceting network database, etc
Okay
and
In OJS
most input output tasks are
Performed asynchronously you have the choice sometimes between synchronously and asynchronous and please always do
asynchronous
But know what you're doing. So every every every every time you call node with
JavaScript
File
node.js starts and it
starts the event loop
Okay, and
sits in its infinite if there is something in the
in
The message queue that is I will talk about it in a minute
then the
the program
Is still running
It only stops when the event loop knows that there are no even
event
That may be possible to
To have in the message queue, okay
So
If I do this
Can you all see my vehicles?
So if I do this
Do you know what will happen?
Okay, everyone agrees
Yes, so the
JavaScript will the engine will see this and will execute this so I will see start in the
In the console then it will execute this
okay, and
When it is resolved
It will add this on a queue I
Purposely don't tell you how it is named how to call but this is
This enter a queue, you know what a queue is
in any language
Computer programming what the queue is, you know the first in first out. Yes
Okay, so the queue is a fee for
So the first input will be the first output
The first in the first thing in will be the first thing out
Okay
So
Console log start then this then this will be added to a queue
Then this will be executed and I will see and in
The console and then this will be executed
because it is in the queue and the queue will be
Will be read
On the same order that it was
That some
That the things are be are were added in it and so I will see promise resolved after okay, perfect
We'll talk about set immediate
Or we like okay just just so you know, you know set immediate
Yes, it's almost the same meaning that
So you see that
You see that I have start and
Then end and then promise resolved and then set immediate executed
Okay, why because this is in a queue that is called micro task queue and
this is
Added in a queue that is called macro task queue
Okay, so every callback of any promise will be added to the micro task queue
And why is it why is it important it is because
the micro task queue will be executed as soon as
The the call stack is empty. Do you know what a call stack is?
Yes, yes you can see that way
No
You know what a stack is
Yes, exactly. It's a default. So the last thing in will be the first thing out. So I
will
Exactly
Exactly. That is what I want you to understand and this is really important and I will try
Because they are not
I will try to yes this one
Yes, yes, yes absolutely
just a minute and then
You have it on the on the course
So here there is a visualizer
visualizer of
The microtask and the macro task queue I
Sent you the the URL in the chat
You can access it
So if you press run
Then you have all the the four functions that are created then the function log a is called
Okay, so this is the call stack so the log a arrives and it's the first out
Lasting the first out then you have set timeout and so set timeout should be here on the call stack and
This is not quite correct. You should have seen set timeout here in the call stack
Do you see my no you don't you don't see my
my mouse
Mouse cursor, right?
Okay, okay, perfect. So this is the call stack and
Then you have the promise resolved then log C meaning that log C
Will be added to the micro task queue
Okay, and even though there is set timeout zero here and even though it was
Called before this callback
Log D will be here so last in first out and
Then the log C will happen
Okay, even though it was called after the set timeout because this is added in the micro task queue, which is
Which has priority?
on task queue or
What should be called micro tasks you okay is another visualizer? Why because this one is
But first it's not quite because you don't see set timeout here in the cost that we should should be
able to see it and the second thing is that
It is wrong because here
It is not added directly to the task queue it is first
added to the
Place that we call web API where all the web API are
Placed and
After zero milliseconds, which is never zero which is at least four milliseconds
It will be added to the task queue and so there is another visualizer
And you see here
If I take this I take the same code
Visualize
If I click on visualize and then very slow visualize so this there is the call stack
With lock a and then so yes set timeout log B and you should see here
Set timeout. Okay
Then is added
It should be should be here still
So this is oh yes, this is the callback queue meaning the
Micro task queue, okay
I repeat so log a which in itself calls
Console log a and then set timeout
What this is very very slow? Sorry and timeout so it goes into the web API
And promise resolve and
the log C will be added to the
micro task queue
So you have log D and
Then log C. Oh, yeah, but it doesn't know the log C
So this visualizer doesn't know about micro task and micro task queue. So
That this one is not really correct as I said and this one is not
complete so I hope you have a better understanding still with those visualizer about
micro task you and
Micro task and macro. So this is micro with an I and this is the
Task you are the macro with an a macro task
Okay
So set timeout and set immediate and set interval
Will be added to the macro task you all their callbacks
I mean will be added to the macro task you and the promise callbacks will enter the mic micro task queue
Okay, and the micro task you is has priority higher priority than the macro task you
Okay
So if you have console log start then set timeout with zero
So technically internally this will be for okay non zero
Then you have set immediate then you have next tick which I have not talked about and you should not
You should not
Need it
But just so you know
Next tick is
Has a lower priority of
Of them
Against the micro task but higher priority
What so
You have micro task you
macro task you and
Next tick queue and this is just because you might see it and
Some some code might I don't know and just so you know next tick has a lower priority
Than micro task but higher than macro task
Okay, so you will have start and end because these are these are synchronously
then the promise
then the nest egg and then all the
micro task
callbacks
Okay
So an exercise for you since you know that the execution order is first
There are currently executing functions then the micro task you then the next to queue
then the micro task
What will be?
The output of this code you you have it if you
Everybody has the open the discourse in their browser
Okay
So
Interval
Yes, yes
I don't remember but it is specified somewhere
And this shouldn't this shouldn't this should not alarm you in any way you you won't have problem with this
Okay, it's just so you know that you can't you cannot
Really have an immediate
Callback
You want to call back it will be called as soon as possible but at least four milliseconds
Exactly exactly. Yes
Depending on how the
Main thread is occupied before this. Yes, it will be at least 10 milliseconds, but maybe
100 milliseconds
Sorry, could you beat the same queue
Some instructions
Yes
But it can be quite tricky so so this is an example the exercise that I that I
That I'm asking you to consider is quite tricky there is one trap
Exactly, yes
Yes
Oh, yes, I think it's because you're using Firefox are you
Yes, unfortunately the the VMS
Cannot handle the copy and paste from your host. I mean your your own
Operating system to the inside to the VM inside your browser only Chrome can
Welcome but you can in your VM
Go to open the Firefox and
go to the
to the the course with with it and then copy and paste in your VM
Yes
Yes
It's strange is going on because the
next thing you seems to be
Having the highest priority, but
Yes
You're scaring me oh
Yes, you're right amazing that is not what I expected
And that is not the result I had
Oh, yes, thank you
It seems it has changed between no 18 and no 20
Sorry, but
Yes, yes
Yeah, so it seemed that I am wrong and I am sincerely sorry
so the next take
Has a higher priority than the micro task
And this is not what I
Yes
Decides from which cube
Something which
Examples we have the
Micro right from the promises which is also adding promise one promise two promise three
Then switches to another cube no, so
So here is the the call stack, okay
Then you call another function which calls another function
Okay, this is a stack so the last thing is the first out, okay, so this function
finishes
So it goes out of the call stack and this function finishes
Then it's out of the call stack, but this one
added
something in a macro task queue, so it goes here, okay, and
This one finishes
Okay, so it goes out of the call stack the call stack is empty so the micro task here
He is
So the the micro
Task you here comes in the call stack
Okay, and then this function calls another function. So here you have another call stack. Okay, then this function
Finishes goes out of the call stack and this one finishes and so it goes out of the call stack and then
You have the micro task
Queue that has another event here or another message and so it goes on the main stack
the call stack
Okay, is that that clear?
What else yes, I wanted to to talk about
The order of the promises that was the trap
Why is it that I have promised three before promise to?
It needs to be resolved first because the promise must be resolved and then it needs to be resolved to
Okay, everyone understood that
Yes, exactly after this is
Executed then it's it can be resolved and this is added to the micro task queue
Yes, so this is added to the micro task queue before this
This is why you have promised three before promise to that was the trap and that I'll say okay. So perfect
Thank you very much everyone
Things that was quite quite a bit I think
I
Suggest that we take a five-minute break
And clear your mind. Okay and stretch, please stretch and do do something on run run
Run a bit. Okay
Drink and see you in five minutes. Okay
Five minutes yet. Okay. Is everyone back?
Yes
Oh, that's a good question, I don't know I don't know I I rarely use it I don't know and
But no, no, no, no since it has higher priority than the microtask it's it's really
Happening as soon as it can
Well, yes a second exercise the last exercise about promises
This one
So if you have this, you know, what will be output
What will be the output in the console?
No cheating allowed
You have to know that this function that I am underlining overlining maybe
That I'm selecting is always called involved synchronous
Exactly, okay. Okay, Marlena
Okay
Sorry what what what comes last
Yes, this callback is added to the microtask you
Immediately so this is called immediately this is called immediately then this is called immediately and
Then oh
Sorry, this is called immediately. This is called immediately and this is added to the microtask and then
This is called
then the
the call stack is empty and
the microtask
Has this one in
In it and this is called
This
Yes, when this is called
This function is added to the microtask it and that was your question or
Only when this is called this is added to the microtask
So about the event emitter interface
So the
Event loop is quite
It's really important to know
In node.js and another thing to really know well
In node.js is the event emitter interface
Many of the native built-in objects of node.js
Implements the event emitter interface
Okay
the event emitter interface allows object to emit and
Listen to
Events, okay, so you might know this with it's really similar to the
HTML element add event listener, okay
The the API is really really similar
For the core features are
It allows for even driven communications
that means that object that emits that emit events are
instances of the emits even the amateur class and
they can
emit and listen to events and
Thanks to that you can call
Functions that are asynchronous callbacks with the
Thanks to the events that are emitted
So you might know maybe some of you know jQuery and on on and off maybe
So this is really similar, okay, and there's a once that
Allows you to only call a callback function just once just the first time even is emitted
No, it's separate no it's separate
We will see it I will answer
But you will answer your question on your own with the things that I'm gonna say
here you import
For everyone do you everyone know about ESM meaning ECMAScript module
meaning the
import export syntax
Maybe not Ference
Okay, so this is the syntax that allows you to import a
Module and
If you see note
column
As this one it means that events is is a native
Module inside note and you don't have to install it
Because it's already in node
Okay, the node
column is
Optional but I strongly recommend you put it in every import that
You you make with built-in node.js objects modules and
Okay, and this should be a rule in your yes lint rule
You don't know is it I will talk about yes lint in
Maybe not today. I don't know
Yes, and no
You know that there are no oh yes
Yes, you can call it a class. I am I don't like to
to talk about classes in JavaScript because
They they don't really exist
Do you know why I say that classes don't really exist in JavaScript?
Because what we call classes in JavaScript are this really functions and
In other languages classes are really like blueprints of
the house and the object is
the house itself
Okay in JavaScript
There is no such thing as a class in
In the way that there is no blueprint of anything everything in JavaScript is a value and
a class in JavaScript is actually a function that
You can only call as a constructor
Meaning that you can only call this function with the new keyword
Okay, when when here
You see the new keyword here it means that event emitter is a function and
Here I call it as the constructor. Okay, this is one of the three or more ways
JavaScript call a function you can call it with new and if you call it with new this function is called as a constructor and
it has implication with
The the way that these
This keyword is bound in the function. I don't want to talk about it in detail
but if you don't know about how
This work in JavaScript that this keyword works in JavaScript
It can be difficult at first
Okay, because not because it's complicated it it is a bit but because it's not
like all other languages
Because the the this keyword is bound dynamically and non-statically
So if you don't understand what I just said, it's no big deal. Okay
So I
Import event emitter from events events is a module built in now.js
Then I create an emitter
Using the
constructor event emitter then I register an event listener with the
function on
Okay, which is a property of any
event emitter and
Then I say whenever event
Whenever the event called event is emitted call this function so on my emitter if I use the
emit
function
with the
event named event
Then this callback will be
executed
Yes, exactly
Yes, this might not be yes, this is not a good name for an event
Okay, so here I can
Okay, so my event
So I register
This event under my event event and whenever I emit my event
Then this callback will be called. Okay
Okay, so now an exercise for you
If I add the following code at the end of this one what will happen
This is my event I forgot to fix it here so this is my event sorry, what did you say?
After this one so you can
You have an error
Oh, yes, yes, I know okay. Thank you. Thank you
So
Okay, I will have to talk to you about
Okay, I'll just
I'll just explain what what yeah, let me just bring and then then we can
Talk about your error because because I know where it comes from. Okay
I'm just finishing about even emitter and then we go to NPM and then
About your error
So
What I wanted you to know is that
Since I have emitted emits here my event
Then I will see an event occurred
Okay, then in two seconds
Meaning that the node will not
Not close until two seconds
Because I use setTimeout and to 2000
Meaning that I will add in the the API the node.js the host API
this function and
I will tell him to add it to the message queue
meaning to the micro task queue in two seconds and
this function will be added and
Will be called so I will see before a second event and then
This will be called so I will see an event occurred and then after second event
Okay
Yes, this is synchronous and that is why
I told you a few minutes ago that you will answer your question yourself because
Since I see before second event then an event occurred and then after second event
Then you see that this is called synchronously. I mean the callback
Associated to this event is called synchronous. So it's not added to any
queue
It's called synchronously immediately. I mean
Yes, exactly, yes, yes it is
Yes
The the call all the callbacks associated to the event will be called
In the same order as they have been registered
Yes
Okay, but you can try you can try to add different
Callback functions to the same event and see what what what what what the result is
So
Bit now about method in properties
So you have emit on event emitter that triggers an event
You have on that adds a listener to the specified event in the first
parameter here once that only calls listener the first time the event is emitted and
Remove listener with the same event name and the same listener so you will have to have a
I mean a
Yes, I don't like to call that reference in JavaScript
But yes, you will have to have a name of the function here and not a new function
When I mean by that and it's really important. I hope you can see the
my
My VS code here if I do that and then I do that
This will not work as expected
because this
Callback is not this one. Okay, this is a new function
Even though it looks the same function. It is not the same
They are two functions that do that do the same thing, but they are not the same
Okay, so if I really want to use off I
Have to first add
Cost of my
I
Have to first add a variable with the function
And then
Then this will work as expected
Okay, because this and this
Will be the same value meaning the same function and
This will be removed this listener will really will be removed
Okay, everyone, okay
Okay, so a bit about best practices
Error handling you you will use this
and
Always listen for a special error event of any event emitter
If it's emitted and not handle no JS process will crash and you don't want your no JS
process to crash
So, please always listen for error event
Avoid excessive synchronous work any even handler should perform a minimal work synchronously, okay
But it is called it is called
Synchronously, okay, but in the function call is synchronously you can call asynchronous functions and you should do that
okay, and
Every time you add a listener you
Take the risk of
memory leak and
Use more memory
Okay, even to emitter is
internally in node
used extensively
Okay, and
For example the the global process variable
Is
Is for an object that extends even to image. Okay, this is
12 noon and 20 for you, right?
When do you want to go for the lunch break now or in half an hour
Okay, perfect
So NPM I
Hope you have heard about NPM
So historically is the it's the node package manager but since it's includes numerous package that are beyond node
It's a joke
Was joke I think they
For a time here at the top left
there was a
Rotating
Just I mean play of words with they added three words that
Were starting with NP and M and that didn't mean anything
Unfortunately, they stopped with this joke I was quite fond of it
But anyway, so NPM what is NPM do you know what NPM is
Yes, is it only that
So first it's a company and it was bought by github in 2020 and
If you don't know github
Had been bought in
2018 by Microsoft so Microsoft is really really really big in the JavaScript community and
In the open source software in general, okay
Please know that Microsoft owns LinkedIn
github and through github NPM and
VS code is also created and maintained by Microsoft
So
besides a company
It's a website with NPM JS com. Okay, and
this website
Allows you to see the registry which can be
the biggest open source package registry ever
in any in any language
Okay, and it is also a CLI. Do you know what the CLI is?
Yes, command line interface or command line interpreter, yes, so the NPM CLI
They are installed when you install node.js
that was not case at the beginning of node because node and NPM were distinct projects and
as I told you
node.js was created by
Ryan Dahl and
NPM by Isaac Sluiter so
It it since a few years after the creation of node and NPM it was
Always
Delivered package
Together but it was not the case in the first years
So the NPM CLI can be used to either
Create minimal package JSON and that's what we are going to do
with the command NPM in it
You can use it to install dependencies that are listed in a special
The special file called package JSON
Install new dependencies with NPM installed in the name of the package to run standard scripts like NPM start
And some custom script and if you want to run custom script you must use
The run prefix
Okay, so NPM run and then the name of the script do you know all this or is it new
Okay, perfect
So now we're going to to really begin a simple
Project and
I wanted you to create a new directory, but I have done this for you and this is called media API
Okay
Then use NPM in it
So you are going to open the terminal and
And execute NPM in it
If you do so
You will see
That it asks for the package name so you can call it me
And you and you see in parentheses the defaults since the default is okay
you can just
Type enter, okay
Then you it asks for the version
And what that zero that zero that's okay. So enter
the description you can
Write restful API for
Movies and series
The entry point will be src dash slash index
dotjs
And not index.js
The test command I will I will talk about it later
Get repo
Nothing now and keywords you can add anything you want I will add media
Movies series
And that's it okay author
You can
I will add myself but you can add maybe the name of your company
And
Between
How you call this I don't know the garden is for this
This character
You can put your
Email address and the license is C meanings meaning means that it is open source. Okay
And then you can see that it outputs
Adjacent file content and it asks you if this is okay and this is okay, so I'm going to press yes enter and
Now you can see here that I have a new
Package isn't fine with all that I
Entered okay
Okay
Now I will ask you
Everyone to get an API from
OMDB API you go to one DB API calm to decide
Then you click on free put your email name your first name or last name and you
Here you can make right training
Everyone has a key an API key
Okay
So now you copy paste this code
And you replace the dots with a real code I
give you
What do you need ten minutes maybe though, what will you need do you know the fetch API
Okay, so we'll use it you'll use it here and
That's it
Yes
Yes
So you will need to know about the fetch API and to know about how the API works
and
API
He works this way. So you have this
okay, and then you can add a
Parameter like by search and s you will use t s and
The API key, this is too obscure. Let me help you. I mean that the URL
Will be something like this
Okay
So there you have your API key
And it you go how you
put that here and
the search is a
Keyword or several keywords after the s equal
The the what the what party yet after the key
Yes, this is as for search and this is a keyword you can you can
Yes, so you can call it Batman or love or or horror
Okay, I see people that don't know how what to do with the URL
Okay, so I'm gonna talk a bit about the fetch API
So the fetch API
Is an API that were first built in the browsers and has come to node.js also
so
You can
You can fetch something
with the function with the function with the name fetch and
by default it calls
With it does an HTTP request
with a get method on the
URL that you put on the first
The first argument, okay
This returns a promise so you can
Use the then method on the return of fetch and
The response will be in JSON so you have to transform the body of the response into a
JavaScript object with the JSON method on response
And this also
Returns a promise so you can have this
So here in data you have the result you have the body as a JavaScript object
so in data
You have something like
Something like this
Okay, this is the raw data and since I'm in Firefox developer edition, I have a JSON
View which is quite agreeable to see much better than raw data
Yes
And so
The return object in the JSON is
Has three properties for a search then total results and then response and
what I
Interested in is search shows are the title that I want that is almost perfect blood
Oh
Yes, you can do that but it's not what I asked but
That's not that's not the most important part
so
Just instead of the I just put s and and just put I don't know and what you
And instead of TT
Put just an a word
Yes
No, this is the right way
Yes, you should put I don't know yes, that's great
Okay, I won't just search
So I'm gonna give you the answer
Who is that
Attila great. Yes, something like that. Okay. So first
So first how can I put the API key here there are two ways I
Can do that
Okay, I hope you're watching my screen
I can do that. This is quite old-school
It works no problem
But I would rather
Use the template string
Like this one, okay
So here I have back ticks
Okay, and this is called an interpolation
Okay, what is inside the dollar dollar curve brace?
dollar opening curly brace and
Closing curly brace. This is an expression
It will be evaluated and then
Converted to string if it's not a string and then concatenated
Yeah
Okay
Then I make the fetch request to this URL that I just built here
I receive a promise that contains a response this response
Is a is an object that has a JSON method
that I can call to
Transform the body of the response into a JavaScript object that I can manipulate
Okay
so in data I
have as I said
the
the the content of the
The thing that you see on my Firefox on my browser
So I have a property called search
with some of the results
Why do I say some because I only have ten results and the total results is?
22,000 more than 22,000 so here I can do
Then
Titles host titles equal data dot search
Okay
And then I can console log the titles and what I want you to do Vlad is to
Get the this code and put it in a then callback
Okay, that was the main thing that I that your code was lacking I know maybe you did it before I
After I went from
And this but that was the main thing that I wanted to modify your code
And so if I do this no
Yes, I
See all the titles wait do you want the answer in the course maybe
Or I did
So, you know, yes, I'm gonna tell you about another way to do it and
I will get an error that will be almost the same as you had
A few minutes ago Vlad and I told you I will tell you about this error
So I
Can do this I can do an I can write here and wait
What does this thing do is that you wait wait for the resolution of this promise and
the resolution of the promise goes into the body variable and
then I
Can do that
Okay, this code is correct, but if I
Executed it
will tell me a weight is only valid in asym functions and the top-level bodies of
modules and
This is really important
Okay
you
Maybe
You had a similar error when you when you tried this code, right?
Why
Because
It also
Requires to be a module and what is a module is a neck masquerade module
This is just a common js module and this is default in node js
so what I will
urge you to do in every project that create from now on is to add here in
packet JSON
Type module and not common js
Okay, you see here I only have two choices common js or module please just use module
and
if you do that
This code is
Running without their okay, man
So again, I just added type module in package JSON
Please do
Do it every time you create project one other way though
Is that instead of
Instead of
Instead of
Instead of adding the JS extension you can add mjs extension
It's called Michael Jackson, yes it is and if you do that and you without
type module in package JSON
It runs smoothly also
because if you
if you name your
Your your file with the mjs extension
It tells no j that it should be treated as a negma script module
Okay on the opposite if I add type module
And I want it to be treated as common js
You write the extension cgs
Okay, and here I will have the same error okay because it's not a module okay
So, please just remember to type to add type module in your package JSON
So now
If I run this or this is body
Okay, just with this await this await
I repeat await for the promise to be resolved and
The resolution of the promise will go into the body available
By nine from lines eight and ten
This returns a promise but this with the await but this await
means that it
It will wait for the resolution of the promise
before it continues here this
this is exactly the same as
Okay, just a minute just a minute
Okay
This part and this part are rigorously the same
Yes, this is still asynchronous
Yes, you can see it that way yes, it is a syntactic sugar yes, it was added in
2018 I think
What is undefined
Who's talking
Okay, you must use back ticks and I don't know about your keyword so back ticking I
don't know
Yes
One
So you can click on reveal answer and you will have my code
I didn't made a mistake
Okay, now it's now it's okay it's fixed and here
Okay for this to work sorry
For for this to work
The this
Sorry
Yes for this to work that this
file must be read as a module so either
use type module in package JSON or
Name it index dot mjs
Okay to be able to
Use the awake keyword just tell me
From the first option way way the second one I'm
Once we have the result from the
This one also this this one also will be executed only when this
Promise is resolved because of the await keyword
These two code are rigorously the same
No
Okay, I think we will stop here and after lunch we will do almost the same but as
a
HTTP server, okay
Then we will
just with the the the HTTP
Native node.js module then we'll try to
refactor it to express
And then we will refactor it to use fastify which is the
Web framework that we will use throughout of the whole training. Okay. Do you have any questions about all we did this morning?
Okay, so we saw about
Some of the
Some of the vocabulary that we might use and it's really important to know it to be able to discuss about our code
with others and
so that we can understand the
Other others when they talk about their code. So I'm talking about what's an object in JavaScript
What are types in JavaScript?
That console is not in the
Atmoscript specification we saw that we saw that the dot is a property accessor
and we saw that
In JavaScript an object can be a function or an array or a literal object or an object literal
Sorry, or some built-in
Some I mean native JavaScript like date or math
and it can also be a
host object like console
we saw that
the code that we write in JavaScript is
executed in only one thread and
We can mitigate this that can be viewed as a limitation
with the blocking and non blocking
Operation and almost all the operation we do in JavaScript
That can be done in a non blocking way should be done in the non blocking way and I mean
I that that it should be
Used every time we can with the asynchronous
Callbacks, okay, and we saw the event loop that it's an infinite loop that checks for and
Dispatches events we saw the different cues microtask you and microtask you are really important
Next excuse not really important. But just so you know that if you see it you
You don't
You know be so surprised and
What did we see we saw the even emitter interface and we would see a bit about it later this afternoon
and
we saw about NPM and
Create a package JSON that has minimal information about our project and
created a
Simple
Index dot JS file that we
Can use or not in
necroscript module way
Meaning that if you add type module in package JSON
It will be treated as a necroscript module and you should always use this
syntax
Okay, so have a good one. See you in
Seeing what half an hour more less
Okay, perfect see you
Okay
Exactly
Exactly, yes this this means that
Sorry, I'm looking for a word in English
No, it's the
Tonke
Until
The promise return return by
Then here is resolved the
This is this is where the the code
Blocks, okay, but it's not blocking the even clue just as you said
Meaning that if this is done if this is done in a
In a
In a in a in a request
Well in a in a route handler
We will see what is your what the route?
Routender is but if this is in a code
That is executed if
Somebody makes a request to this route particular route
If during the the fetch another request comes in
Your application can can treat it can process it until it has the response
Is everyone here back
Okay, perfect
So I told you that I would like to rewrite this
So that we use the built-in HTTP module
Before that maybe
We want to
Yes
Maybe we want to not
Put this
In clear text in our code base
Because this is this might be sensible data
sensitive data
Okay, so
What I am proposing you to do what I'm suggesting when I'm suggesting is that we put this
In what we call a dot of five
So we are going to create in the
route in the root folder of the project a
dot of five
You see then this dot of has special icon. Oh
By the way, you see these icons because I installed the extension VS code icons on your VS code
Maybe you don't have these icons
Okay on your on your computer birth on the VM you will have the same
And so we will add
DbAPI key equal and
Your API key of OLDB in the dot of five
Okay, everyone did this
I don't know where you are. Did I lose you?
Maybe I lost your
Could be true
Oh, yes, yeah, okay
So yes dot dot
Neither do I yes, maybe the package is not nice don't know
Okay, maybe I forgot so you can press ctrl shift
X and this opens the extension
part of the sidebar and
You can search for VS code dash icons
Yes, oh yes and here you on the
Jolt don't touch anything on the on the upper side of on the top of VS code you can click on VS code icon
Yes, and now if you use your explorer so shift ctrl E
Okay
We're just using the an extension that makes the icons prettier than the defaults
in VS code, so you should press ctrl shift and
X and
It should open the extension sidebar. Yeah, then you look for
search VS code dash icons
And click on install oh it is installed so click on it maybe
And then use it
Set set
What does it say the first button on the the first blue button
Yeah set file icon theme, yes
Press set file icon theme and then click on VS code icons on the top
And now if you go back to your explorer with shift ctrl E
E like Explorer, okay the first letter of Explorer
Yes, well you should create it dot of to create a dot on file
No, you can but I think it's simpler from the explorer of VS code
Who is this
Stefan go back go back to VS code icons
And click on set icon theme
Yes
And click on click on that at the top click on VS code icons. Yes
Where are you
Any good
I don't see we
Okay, this is perfect
Just write one DB
underscore API underscore key equal
Yeah, and then
API key
Without cause yes, this is not this is not a JavaScript file. This is a dot on file and you should have
You should have
Syntactic coloring
And you should just like me you should have the
Where am I that you should have the equal sign in red
Maybe I forgot this one too. Sorry, you should have the dot of official
So shift control X dot of and should be the second one
This one yes that are official from that of that or
It's not red but change the notes
Black bells
Yes, it's on purpose and you can click on the link at the top of the
just above the
Your line and click auto toggle auto cloak just above the line just above the line
Yes, maybe you don't have my theme
Yes, maybe you don't have my thing to change the theme maybe I
Should have told you more about VS code. So let's talk a bit about VS code
Okay, so I told you this is the sidebar and this is the activity bar
okay on the either on the right of the sidebar if you have the sidebar on the right or
At the left of the sidebar if you have it on the left. Okay, no
Okay, this is the the activity bar. This is the sidebar
with
control J you can toggle the
Panel so this is the panel. This part is called the panel. It has it has several tabs in it
Okay
the
the problems
In the problems you have all the
the the either the warning or the
The errors that it has detected on any of the file of your project that is open currently
Output is for the on the extensions
They can output some some some information
debug console is to
Just like the console log of your your
browser
terminal is the terminal ports are a way to
Forward ports I won't talk about it
Open shift. I don't know what it is. I don't use open shift and
And
Commons I don't know this one. This one might be new
Okay, so really important terminal and problems
Sorry
Oh, yes, okay. Yes, maybe it's from extensions
What else what else what else
Yes
Some shortcuts so so you know about control B
to toggle the sidebar control J to talk over the panel and
Then you have shift control E or the Explorer
Okay, this is the Explorer and you can see all the files of your project here
it has
an outline of the
Current
Open file you can
You can go through it to
go
Quickly to any part of the code the currently open file. We have a timeline that
Saves all the different states across time of your
file
Yeah, the open editor I don't use it I don't like it and I don't know how to get rid of it
It was simple in the previous
Versions now know how to do it
Draw I don't know what it is and so knowledge is from my extension smiling so don't pay attention to that
So this is the first
Combined shortcut that you must know is shift control E to go to the Explorer
Then you have
What is your shift control F to find and it does not only find can replace and
The first time you open it
You have only this here if you click on it. I
Hope you see all my VS code
Here if you click on it it makes the search
Sensitive case sensitive if you click on this, so let's say
URL so here it detects the four
The four occurrences of URL, all right
And if I click on it there is only three because this one is not
With the same case is lower case and this is uppercase. So it does not
Find it
if I click here I get all the the URL, but if I
get the L
Does not constitute a whole word
So it doesn't not find it
Are talking to me
So control E and control shift control E shift control F find and here you can click on the
on the
Don't know the word list or this particular character. So if you click on it
You can have an input to put something to replace it. Okay. Oh, by the way, this is to use
Regular expressions, do you know regular expressions?
Really really useful
Should all know a bit about
Regular expressions so here you can replace and here if you click on the
three dots here you can
Tell this good to only search certain
Certain files or folders and can exclude some files or folder folders
In your search, okay
So this is shift control F to find then you have shift control D to debug and
We will use it
later not today
Then you have shift control X
To go to the extension and the last one is shift control
G
G like get so it
It shows in the sidebar the
Everything about the git or bazaar. What do you use? What SEM do you use you use git or bazaar or Mercurial?
Okay, it's really important to know about get it's it's quite complicated
If you're interested I can do training also in jib so
So here are the five
Shortcuts that allows you to go to the five
Different parts of the sidebar
Matching the five icons on the activity bar that you have by default. Okay, the first five ones
Okay, I
Had some extensions for you such as Docker and I
Added I think shortcuts to go to Docker if you type shift control D. I
Think oh no, it's at at sorry. Sorry. It's at the
Okay, um
So what was yes if you hit the alt D you should see the Docker view
With the containers. Yes, so did it
Okay, so this will use tomorrow I think I hope
So there are plenty numerous and almost an infinity of extensions
with for VS code and
And I just
Install some of them. Okay, I so I
Did not install all the ones that I use
But all the one that really matter for the train
Okay, well I hope I hope I did but
so VS code was installed but not
activated right and
Dot was not installed. Is that correct?
Okay
okay, so
now
back to
our dot on file
so this is
My on file and how do I get here
The environment variables that are declared into the dot on file
This is the way
Process dot of
That
OMDB API key so
This here at the right of all that
Must be exactly the same as in your dot on file. Okay, this is exactly this right?
Now
It won't work
Natively so when I run node
If I do this it will say the API is not
Not okay, so I will have an error here I think
So I added a catch
Why those who works for me doesn't
So who for who does it work still
What
It should not
Yes, that's that's that is what I was expecting okay funny really really
Yes, that was what I expected
so
Tell me the
The version the node version you are using for the not yes
Okay
And you're sure you have the save the file
Okay
So to make it work
There are a few ways
Okay, the first one is not the one that we will use but I want you to know it
So the way that we're going to do it is
To use the dash dash of
Target of the note
And I think this is or maybe it's on fire
Yes
Okay, so this is one option
Just add dash dash on fire and the path to your dot on fire. Okay a
second way and
This is why we are still in the NPM
Chapter is because we are using we are going to use NPM to install another package
To handle this dot on and it is obviously named dot on so to install a package
we I'm going to show you your my package JSON, okay and
just
Pay attention to these packages in here. Oh, so you told me you don't know about git
much, right
Okay
Okay, how do we create a git repository
Okay, so we're going to use git init
Just git init and then what it what it what it creates is a
dot git
folder
In which there will be all the database of the repository
Because you can see a git repository as a database of contents. Okay, so now you see
For for those that made them that executed the git init you see now that your prompt
is now
Has additional information and
The you have
Background color which is yellow then
A character here that
That is like a branch of git or
a road sign
to tell you to to to go right, okay to go on the right lane and
And then you have master right
Master is the name of the current branch. Okay, so what I want you to do now is to
Add
We're going to add all okay. This should not be done
Should not be done
Generally this should not be done dash dash
Capital a should not be done. Why because you are adding everything and
This is not a good practice. Okay, but for simplicity and because it is the start of the project. I'm gonna do it
So you see here that
You might you might
This this
interface, okay
So maybe it's your it's in English or in your language. I don't know
So sorry git status
status if you
type git status
You you have the status of the current
state of your repository
Okay, so it says no commit
It says you are on the branch master maybe yes, you are on the branch master
there is no commit and
these
addition or modification or changes yes, and
Those changes will be committed and this is all green
Okay before
Sorry just a minute. Oh
Oui
Oui, je suis en formation. Oui, j'ai mangé mais tu peux
Tu peux venir et commander ce que tu veux avec mon compte pro parce que je vais manger très vite donc j'ai recommandé
à plus
So
Before I add the files in what we call the index
Okay, it was all you should have this in red right
You can yes
But I don't think it helps you to understand
Git
But yes, you are right. You can use
if you will all of this and
So if you want to add all you click on the plus here so this plus
Means exactly the same git-git add-capital-a, okay
And now
Now you should see this everything is green and is ready to be committed
Okay, so now I will commit
git commit
Dash M and the message
message would be
Meet NPM project
Okay, and then you see that the background goes back goes from yellow to green
If you have the green it means that everything is
Is
at
Every file is at the same state as the last commit
Yes, thank you yes, thank you so
Sorry for that. Yes
so
You are going to do git config
dash-dash global
user
Dot name again your name
Your name so Stan
Okay, and then get
config
global
user email and then your email
As an error
Oh
It's dash M like message not N
It's git commit dash M like message
Okay, everyone is okay everyone is green almost everyone
Okay, so
Now you will
Add package that is named dot on to do so you will either
Write NPM install dot on or since we are all developers and we are lazy just
NPM I dot on I standing for install okay, and here you go
now
You should you should see two things first it
Goes back from green to yellow meaning that there is modification in your working directory
Second that you have a plus here at the end of the
After master
You have a plus and
Plus
Plus and minus sign okay that means that in your current working directory
there are some new files and
Some lines that were deleted
Okay, and if you write get if you have this one
this line that was
Erased deleted removed and those one that were added, okay
Get diff come difference as in difference
What do you mean it doesn't show
It's almost yes, yes
So it's almost the same line
if you look closely and this is important for the for the for the configuration of yes, it's so
If you look closely there is a coma here this line there is a coma at the end
So it's not the same line
I'm sorry who's talking I can I can I'm sorry because I I am
watching
your VMs, and I don't know who is talking and I I'm sorry I don't
Recognize all your voices. Yes, so Stefan. Yes, so
What's your what's your problem?
Oh, yes use the arrow key the arrow key down down arrow key. Yes
So this is
This is the less
Less program
You know less maybe you don't know click on quit keep you queue for quit the fun use queue for quit in the terminal
and then go
Less packet JSON and now you can use the arrow key the the yes the arrow keys. Okay?
Okay, this is used internally by default by git
for everything
That doesn't fit the screen
It uses less so you can use the arrow key to go down and up, okay
Maybe I should have
So please everyone's watching my screen
so you see here I
I
just
entered git diff, okay, and
Since my terminal is not
big enough to show all the
Other content I
Must use the arrow key down
to go down and when it says and I am at the end of the file and I can
type queue for quit
Okay
Now
The important thing to notice is that when I
When I used NPM install it added
dependencies
property and
in it an object the value of the this property is an object and
in it we have
.of which is the name of the module and
Here we have the version
at least a range of
versions a
Range of versions. Why do I say a range of versions?
this is
Versions specific versions specific versions. Sorry, okay
This is the version 16.4.5 of .on
if in my package.json I do this I will only have this version of
the module if I do this I
Will have the latest version at the time I do NPM install of the
16.4 branch
Meaning that I can have the 6 and the 7 and even 12. Okay
And
This is the same
almost as this
This is the latest this means the latest
of the 16
No, no, sorry this is the same
This is the same as
This okay this tilde here means
The latest of the 16.4.
16.4 and at least the 16.4.5. Okay, and this
Means
This
This means at least the 16.4.5 and the latest of the 16 branch
Okay
By default when you use NPM install
It puts a carrot here. Okay
So now that we do have these .on
Module
Not only did this happen this modification of the package.json, but it also created a nod modules
folder and
in it a .on folder and in it all the
Package
Contents of the .on module. Okay
This is not our code. This is the code from .on package module
Okay
Now how do we use it we use it
simply by importing
.on
from .on
Okay, this is the name that I give
to
Whatever the default export of .on is. I can call this whatever I want
But please stick to the name of the module
And
For this I will load the environment variables with the .on.config function
This will automatically read a .on file at the root of the project. Okay
So I was not planning on
Talking to you about this already
So it's not in the course yet, but I will I will add it tonight, okay
Okay now
Now everything should work as expected
So this is work, okay
And
We then the .on file
Okay
Is everyone at the same
status than I
Or do you need time to catch up?
I will add this
In the course
Yes
There you go
Well everyone has installed the .on module right
Okay, what is undefined show me
Okay, this should be good
What is happening okay, this should be good
Add
Get after the line 13, please add console log
Okay, oh yes
I told you to put the .on file at the root folder in the root folder not in the src folder
so move it at the
You can take it and move it
At the bottom and it will go to the yes exactly
and then you can go back and run node
run node again on the
On the terminal you can run it again and yes
Yes
Yes close the package JSON please
Don't save don't save
and then you go
Ctrl P and P
2 times P and and yes here you go and you have it. Okay, and now I should change it
No, no, no, no
you
You you did not add type module in package JSON
And it says here in the console you cannot use import statements outside a module
You really should know this error means that it is not
It is not read as a module. So you just have to type type
Module yes, perfect
And this should go smoothly
Maybe you did not save all that on your dot on files you see there's a
There's like a pin
Yes little disk, okay
Yes
Okay
You're welcome is everyone okay now
Yes, okay now
So what do we do now
Now you can see in in the course that I will
Have you create a server with the
Node.js native HTTP module, please read the course and then try to make it work
Index HTML no, there is no index HTML
Yeah, you should not delete yours
You just you should just see what I wrote in the course and then adapt it to your index.js
Yes, do you know the difference between the good developer and the bad developer
This is not a trap
Yes, no, I'm don't I really don't like comments
that the comments that I you see here I just so you know the
Statement that I asked me you to do okay, but a code should not have many comments
Comments should be used really scarcely
Why because if you add comments it means that your code is not easily readable and
Understandable as it might want to put some
comments where you do something that is not quite right or is
not intuitive and
Or because there is
Specific reason to do what you're doing to your code then you you you create a comment
explaining why
But but certainly not how
Your code is and what your code is doing just why you are putting it this way
Okay, I hope I hope you understood me
No, so
The first answer was quite correct you don't copy paste code. Yes
You can copy paste code but from the official documentation, okay
so here
To get the URL you should know how to get the euro. I am I am helping you here
Okay
You should know how to get the query string, but do you know what a query string is
Exactly
Exactly, so please please everyone see see my what please everyone pay attention to my VS code
To my VS code yes
What is this
Yes, you can call it that way
How can you call it this is a URL
Okay, what is HTTP?
Protocol perfect what is example?
Yes, and it should be is it every time a domain
It can be an IP so this is not a domain this is a host
Okay, this is called a host
And what is this
8440 the port number exactly
And
What is my route?
Yes, you can call it a route yes, it's called path, okay
And this is
A query string
Okay, this is the query string
This is the path
This is the port number. This is the host. This is the protocol and what is all this this three?
What is it? What is it called? You don't know it's called the origin
This is the origin
Okay
the combination
of
protocol host and port number
Is called origin whenever one of those changes the origin changes and this is really important for
cross origin resource sharing
As you might know as course, maybe you have heard of it
No
So this is really important to know that origin is the combination of protocol host and port number
Okay
So this is the path or the route
This is the query string and this is the hash the hash
The hash is never read by the server
the client doesn't
Send this to the server
only this okay
this won't be in the
In the request on the server, okay, but you can
Get
All of the others data here, okay
So
Here you create a server and here you give a callback to the server this callback will be
Called every time you make a request to the server and here you start
the server and you you by telling him to listen to this port on this host name and
Whenever it's ready
you you
You ask him to
Run this function to invoke this one. Okay
So what I'm gonna do is just here
Command this and
Wreck dot URL and console log URL
Okay, just so you see that
When I run
src.js
the service starts running and
It does not stop
Because it is still listening what having any request, okay
Now I want to show you the
Or I have much to show you I want to show you
The extent the extension that I installed for you that is called rest client
And to see the points of this
Extension I will ask you to create a file in a test folder
named API but
HTTP you can either call it HTTP or
Rest doesn't matter it's it's the same
Maybe some of you know postman
Okay, well postman is quite complicated and it's really difficult to add all the
The
Other collection of requests in the project
because you have to export the collection into
into a
into a file and then
you have to put in the the project and it's quite difficult to
to
to when you have
When you have a conflict in it, it's quite complicated to
To handle them
Okay to deal with them
so this file
Will have the same purpose as postman and it will be much simpler and it will be all in one file
That you can understand and the machine can understand. Okay. Everyone has a API dot rest or API dot HTTP file
I don't think so if you do
if you write HTTP
Localhost 3000 you should have a link
above it that says send request and if you click on it you'd have this
Yes
Yes
Yes
So everyone is okay
Okay, so you have the slash just like me
Okay, and then if you put API
V1 slash
F
And you click on send request you should hear have slash API slash V1 slash help
Okay
So now you see that in rec URL all you have is the path, okay now
Yes, so what I want you to do is to get
The value of the S param in the query string
Meaning that if I
Just do that I
Should see I should get love here in search I would like to have love
How can I do that if I do send requests you see that in the URL?
I have all that I need can someone tell me how I get this maybe I can split the
URL at the
Question mark and to be the second yes, you can do that
Yes, you can do that you can do maybe you
You use the C or C++
No, what what what language have you used I just used a
Okay, so I'm just so you know there is
An object native object which is called URL
So if you see my
My browser I can go to MDN
MDN should should be your go to
Documentation okay, this is French
All right, so I'm going to look for URL and the URL constructor
Yeah
Helps me a lot why because
I can do that and then you see my my
browser I
Can use the new URL?
Constructor the URL constructor to build
the a new URL and then
But I will show you
Okay, so the base URL is not important. Okay, because I don't really need it
so I
Can use this one
API base URL and I only have to
Only have to use an origin, okay
so
protocol host and
port if
What I can use them?
I'm sorry. I am not
It's not mandatory to to
To indicate the port number if it's the default one for for the protocol, okay, so I'm just going to do that and
I
am going to
Create the URL
with
This the URL and the API base URL
Okay, and from there
And from there I'm sorry, it's not in the course yet so
So I have to develop with you
There you are well
You see that I have search parents and search parents you see this is all
Intelligent smart completion, okay, and with the ABC it's not smart completion
Also completion. This is dumb. This this means that
it
Suggests me words that it found in the project. Okay, and here with the other
Icons here. It means that it really has
these
properties
Because it detected the object
Object type, okay, so I want I'm going to use search parents
and
Here I can get
all the entries
or the keys
or a specific value
for a specific
Parameter
Okay, so the search
Is this
So if I control see the server and so if I
Quit the server and if I shut down the server and then I rerun
With the new code and I send a request here. You can see that I only have love
Okay
This is what I wanted, okay
Now that I have what I search I can use this
And I let you do it I'll give you two minutes to create all this
It's called rest client
Is this one
No, no in the VM that I built for you
But it's not it's not installed by default when you you install the escort
Yes, yes
And you mean you don't have send request about
Okay, okay, just let me arrive where are you
So Stefan right
Yes, it's wreck dot URL not coma
Yes wreck dot URL
So strange
Can I do you think I can take control or I
Don't know how to do this
Interactive
Whoa great good stuff so
Why does it look for oh yes, this is why oh
Sorry
You did not save the fire the fire wasn't safe so so try again
Yeah, go on go on or maybe I can't maybe you can because I'm here
Okay, this is really strange
Titles join
No, no, it's here
Okay
Now it's working
So anyone has a problem with the task at hand
Where who are you?
Yes
Okay
Yes, why is it why is it do you think
Yes
And so how do you get only the titles
This is JavaScript so what do you have in search this is
Please see my just watch my VS code
So in search what you have you have objects, okay with some properties
So, how do you get only the title?
This is a what is the list of object yeah
Yes, it is a list of object
Yes, exactly who told that
Yes, who it was love who told about map
So, how do you use my is everyone aware of the map array method?
Yes
Okay, who does not does not know the map array method
Yes, please do
Yes
Yes, where where does it save it
No in a new array
That the map function returns
Okay, so when you call map on an array it creates a new array with exactly the same number of elements
and each element
of the original array is
transformed or what we call mapped mapped
to a new object
And this new object is what is returned by the callback function that you give to the map method
Yeah, okay. This is this is too complicated. Okay. Let me show you an example
I'm sorry. I just broke my glasses. So give me a minute, please
So search is a
I want to show you what it is
Constraught console.no type. Yeah title
Okay
So if I call it
Your is not defined. Yes
Okay, so this is the titles
That I get okay. It's an array of objects
with the property title that I want
And maybe the year
Okay
So search is an array so I can call map on it
And map requires as
An argument a new function that will be applied to any element
of
The of the original array
Okay, so here here I can call it whatever I want is the element of the array
Okay
There is second
Second
Parameter that is the index. Okay, and there is a third
Parameter that is the original array
Okay, I won't need them I will just need to return a new object
Sorry
to return a string
with el title
and maybe
space
And the year
In parentheses, okay
and this
Thanks to this but I think
No, no
Oh, that's bad
Refactor
Convert to template string
Oh, I may have
Maybe so I selected this okay all of this from el to here
Right click and then refactor then convert to template string
And it writes here for me, okay
So here I have the titles and the year in parentheses
In this function we just called for the map we change the elements
It does not
Will not change the original element of the array right? Good question. Yes and no
so in the map
You will when you call map you create a new array. Okay, so it will not change
The original array
Okay
but
You can do things that you should not but you can
like
In el
This is the element
and you can
for example add a
New property on each element which will be titled with here and this will change every element of the original right?
Yes, yes, this is really important to know and
And
I think that that wasn't the prerequisite but not maybe not as clearly as it should
That you should know
the difference between a primitive value
and
An object value
For example run js
Okay, i'm going to share with you
I don't know exactly how it is internally implemented
So we could have done directly then if you call it on one array, maybe could have done a copy of that array
So somehow to still stop you from modifying the original array, right?
Yes, and no it's it
There is no such thing in javascript as
a
Deep cloning or deep copy, okay natively
Okay, i'm gonna show you this
you see all my
Does everyone see my the the yes, the third the third window that i'm sharing
Okay
I'm writing. Hello. Everybody's seeing hello
Okay, this is a run js application, I mean this is the application run js, okay and
It allows me to write
Javascript and even typescript
And see the result immediately
Okay, so
when
object one
When I do that
And then I do that
Um, what do you think will happen if I
If I do this
Why
Perfect even though the objects seem to be the same object it is a different object every time you use
the color brace
the opening color brace
Like this it creates a new object
Remember, this is exactly the same internally as this
Please do not do this, please do not write this write this okay, but it is exactly the same
Okay, so when you do that you create a new object you create a new instance. Okay, so this is false. Yes
Okay
But if I say if I do this
What will that be
Why is the same reference
Yes, you can remember it that way but
Please know that there are no reference in javascript. There are no pointers and there are no reference in javascript. So this means
this object
The object you created here has two names has the name object one and object three
Okay
Yes
It should you're right
Okay, so this is true
Okay
It should be
Okay
Okay, this is false and this is true right so now if I do this
Uh, odd one and odd two and
Um three
Uh
Everyone agrees
Okay perfect
So this is true
Okay, and now if I do this
Will this be array two should be a shallow copy of the first array
Mm-hmm. And what is a shallow copy for everyone? It copies only the first level of an object but
Yes
Not the deeply nested with the properties
Exactly. So this returns
You all agree because I don't
Yes, so this
And this
and this
And this
Are three ways to access the same value
Okay
What is empty oh no, no
Yes to be exactly the same, okay, and so last if I do this
Map el
El
Okay
Now I have
Three elements in array one, okay and for each one
I create
a new
Value which is the value either of the foo
Property or the bar property if foo is false today that way I have
Uh an array with the string foo and then the string bar and then the string foo, okay
but
if I
Do this
Well, I should not do this
Just yet
Okay
But if I do this they all they all have the new property
Because here
All we have is a complex object
and if I and since every object is writable by default in javascript
I can add a property or modify a property or delete a property of
Any of the elements in map? This is what?
This has changed it not the map yes
Okay, everything everything is okay for everyone
But normally this this thing that you did in line 25 should not be done
No, this is bad this is bad
This is bad and that that is
That is a what we call an impure function
And every function that you give to map
Should be in the realm of the pure the the functional programming and therefore should be pure function
and
since
This modifies
the input
It is not pure. So this should not be done. Definitely. This is wrong
This is definitely wrong
Okay, so I hope that answers the vlad vlad question about does change
the
elements in the
In the original right
Um
I recommend you to test run.js. It has a free
a free
Well, it's free and you can pay like ten dollars to have
to have some some some
Some premium
functional features
But really the the free one is quite great
Okay, so I don't want this I just want to map the the the each element of the search
That is returned by the open m api open db
Open movie database api to just a string
Okay, and so
Um
if I do this and I
Restart the server and I send the request
And here I have
all
The titles with the year in parentheses in an array, okay?
Thanks to this map function. Okay now can I um
How can I
Sorry
How can I send instead of just text? How can I send json specify line 33 that
Text json, yes application json. Oh, yeah
Right, it has changed over the years. So, uh, maybe text json is still working but
application json is good
and then
What do I do with titles?
And I guess we should stringify that with json stringify
Okay, do everybody knows stringify from json?
json is a native
uh a native
javascript object
That only has two properties stringify and parse
Okay, so it can
uh parse uh text
To build a javascript value
Or on the contrary take a javascript value and convert it into a string
So if I do this
and I
Start again
My server and I send the request again
Should have here
What happened well here I go, okay
So
We now know
Oh by the way, um, yeah, I did not tell you that uh, this is all the response
this is
The first is the protocol used
Second there is the the http code
Of the response
200 is okay and it should almost every every
Every time should be 200
For post um for post
Request
I mean request with the post method it should be 201 and this is the text
Status
Of the response 200 is okay 201 is created
Here you have the content type of the response
the date of the response the states of the connection
and the transfer encoding and then we have um
Black line and then the response in json
Okay
and
so
The 200 comes from here
Okay, right head means right header
So you write header and you first give this http status code. This is really important for any api to respect
the http codes
And then you add an object with every header that you want to add
http by default adds the date and the connection and the transfer encoding
But you have to tell your client that your response is in json. All right, so this is quite
um cumbersome to
get the search and if you want the
if there are
Multiple parameters in the quest string
You don't want to go there
through over this and oops
This and this
and
you don't want to
to write all of this and to stringify yourself or this
and so
there are
modules that were built to upon on top of this http module and
And make the life simpler simpler for the developer and that's we are going to see with express
Okay, i'm just gonna write this in the course
Yeah
Yeah sure
when I
Triggered the when I access the the from the browser. I also saw in the
Logs of the node
Can you show me please yes. Yes. Yes
Oh, yes, yes, um this this you cannot do anything the
Your browser needs to
display
For your for your
for your
To give you a good user experience. It wants to display for you the the the fab the fabicon
in the in the in the tab of the browser
And so it looks for the standardized
Fabricone
Path which is fabicon.ico
At the root of your of your domain. Okay, this is
This is the the browser that makes it for you you cannot do anything about that
Um
So I wrote here
If you click on
Reveal answer you have the code that I wrote for you. Okay, it is the same that I have here
um, i'm just
to
Eliminate the constructor
Okay, now we are going to try to use express so first thing to do is to install express so how do you do that?
You all know how it's called express nobody knows everybody knows
Yeah, exactly so npm i express
And since I gave you the hello world
In express, oh I did not uh, I can add
I can add
Also, I did not
Finish about the good developer and the bad developer. Oh, yes
Uh, I did I told you that the good developer reads documentation official documentations
All right
So this is the official documentation about express
And i'm going to add it in the course
Uh, so i'll make express
Here a link
Here you go
So this is a link
That goes to the home of express.express.js.com and here is link about the hello world
I just repeated the hello world here so you don't have to go there
But you should know
that there are many more
things to know about express and if you want to
To know more go to the official documentation
And what I want you to know is that since
Express is more
Makes it simpler for the developer
Is the result of invoking the express
Function, okay
And so here if I
Use
this
The response is hello world
Okay, because I sent hello world
All right
now
First thing first if I want to send json
I just do json
And here
If I stop and restart and then
Send a request again. I have a json and you see that
it's express
Added by itself this header which should not be here and we will
Normally will
Remove it but it adds this content type
It automatically adds content length
meaning that
This is 27 bytes
It adds an e tag. This is for
Cache caching
And the dates and the connection
Okay, so this is much more much simpler, okay
You just give an object or an array and it will
Transform it automatically in json
Okay, and the second thing is
that that when you
want to
When you want to have the query it's available in web.query and if you want just the search
It transformed it automatically
in
An object query is an object and every property is the name of the parameter here
So let me show you
Here search
And if I do again the send request I have searched with love
Okay
So now that you know that I will ask you to refactor the code that you have
To use express and to do the same thing but with much less lines than previously
I will give you five minutes
You think that's okay
Yes, yes you can
So
Everyone succeeded who has difficulties or encounters
Yes
Oh because you have a this is not a node js problem, this is a
Shell problem. You have a quote at the end of node src next js in your terminal just do ctrl c
So
So atila is good
Eniko, where are you?
You are lost you mean
You said you're lost
Okay, you should have told me before
Okay, so
You can please remove the commented code
Okay, okay, okay, okay go go up please so here you can you can comment the import http
Okay, you're not using it
And
So you are importing dot-on then you are importing express then you are using dot-on to read the dot-on file
Then you get the api key from the environments then you create a port a host name that will use it later
You create base url api url search url
Then you create an express app. Okay, this is really important
then
You can comment the line 29
It goes with the the other portion of the code you commence it, okay
Then you add a route which is the
root route
Meaning slash okay at line 49
And you add a route handler which is this callback that will be called every time
the a client
asks for the slash route
Okay
Yeah
I clean a client 50
You get
you try at least to get the
s parameter the value of the s parameter of the query string
Of the url sent by the client
Okay, yeah
Make me look at your api rest file so here in your query string you have s equal love, okay
So the search in your code will contain love the the the string love
All right
You can you can save this file, please control s
Okay, and then go back to your index.js and then at line 51 I want you to make
um
A request just like the line 40 see the line 40. Yes
So you get the body
of the response
from
Yes, perfect
And you will
Create the url it's just fetch
Fetch
Fetch parenthesis and in parenthesis you you you call the url and the url
Is the concatenation of the search url?
Plus search, okay. Why because the search url is the api url
Which is wmdb.com
um
Question mark api key and the in the api key
and then
S equal
Okay, so
Finally, you will have htp
Ww.omdbapi.com
slash
Question mark api key equal uipi key
10 s equal m search
Which means love
Yes
So this will give you a response
and you will
Try to get the json value of the body of this response. So here you go res
Res fat fat arrow
Res dot json, okay, so this transforms the body of the response
from a json
String value to a javascript object value. So in body here you have
um
You have the
the response
As a javascript object and so you want to res json
the titles
Only the titles so you can use the lines of 41 to 43
Oh, it's dollar not
Okay, and you can remove the line 56 and line 55 modify it to send just titles
You can you can remove everything and just
Send title, yes, you can even remove the
The object yes, you can remove the
Yes, the curly braces here. Yes. Perfect
Now you can run the your server and it should be good control c and
Do not
use
the slash
Backward slash on on your terminal
You inserted a backward slash
Maybe you did not install express so npm install express. Yeah, I installed it
But maybe I need to reopen this package
Yes, uh in your terminal press control c and then npm install
Npm
It's just that yes, okay and rerun your server
Uh, it's express not express it's an x
At the start of your module
Okay
Okay, go to api rest and send me request now go to api dot rest the file
And click on send request
Okay, oh
um
The dollar sign should be outside the curly brace not in
Uh, well
Oh
So now, uh, i'm going to tell you a bit more about
Uh
About refactoring in bs code. I already showed you how you can transform
a
a old school
Concatenation with template string. Do you remember?
I clicked right
Yes, you remember I clicked right and then refactor and then convert to template string. Okay
There's much more you can do while refactoring with bs code. The first thing to know is when you press alt
and then
the
Arrow up and down you can move a line you see
Um
Uh, if you are on the line and then you press ctrl c without any selection
It copies in the um in the clipboard
the whole line
So you can anywhere in any line you can press ctrl c and ctrl v
And then you copy
the line
You copy paste the line. Okay control c control v and then you okay
There is a shortcut unfortunately
Linux has the same shortcut for the window management
So maybe you want to change it
But it should be
shift alt
It should copy
a line
Shift alt and then um arrow down it should copy the line and
And put it at the next line
And shift alt
Arrow up
It should copy it
above
Okay, does it work for you or is it
Oh, yes, but this it's it's
it's
important to know so
maybe
And try control shift
Uh arrow down, what does it do?
It it also makes um
Several cursor
Multiple cursor
Ah, yes, and this
This okay, i'm sorry about that
This is uh, this is from uh, you are Ubuntu. Okay, this is not uh in vscode so
uh, you can um, what I do in Ubuntu is I
I change those
native keyboard shortcuts, but what you can do
which might be
better
Is you press ctrl k and ctrl s?
And then you go to keyboard shortcuts ks keyboard shortcuts and then
You can add
Copy line copy line down. So ctrl k ctrl s
Sends you to
the keyboard shortcuts and then you can change the
the shortcut to
For the copy line down action
Or the copy line up action
Okay, so
I had this
and
Uh, I had
And I had
Okay
And I had this
So what I need
Is this I don't need anymore
Okay
This I need
So I will use it
and
Yes
Yes, you're right and
No, um this API base URL should not be used anymore because you're not using this line anymore
API base URL should not be used
Yes, those are those are still uh used
I want to show you where so this is
transformed into just this
Okay, these these two lines
Are transformed into this. Okay, so I don't need them anymore
Okay
This
I still use this line. Okay, and this also
And these titles I still use it use them here
And these two lines
Are just
Becoming this line
So this is used here this is used here and this is used here so they are all
used
All right
I have just one question before uh line 27. It shouldn't be send the status code back as well
Before before what line before line before line 27
Should not be specified the status code like as that status 200 or something like that. Shouldn't we do that or
You can
You can but by default
It's a good question really
And thank you for writing it
um you can
here
that is
And then you can do this
Okay
but
If it's 200
You don't need to
If json does not have any status, uh numeric, uh status code it will send 200
So it is important only for posts and errors, okay because for post I repeat
For post we want to send the 201 status code so that it is created
Meaning that whenever you do a post
Whenever you do a request you make a request with the post methods
You create a resource and you want to notify your user that you created
um appropriately the
The you want the server
To tell the client that the resource was created has been created. So you send 201
I see and that means that the rest of json is enough. We shouldn't put rest of n or anything like that. Yes
Yes
Yes, uh good question. Yes, uh, this this means
The app dot get means that this this handler
Is a handler for this right this route. Sorry only for get method. I mean only for requests that was were made
That will be made made
With the get method
So
Sorry, could you repeat
Exactly, yes
Not necessarily you can it can be a put method or a patch method
We will see we will see all of that
But post if for creating resource and put and patch are for modifying a resource editing a resource
But technically you could also use the get but not be able to
Practice right you you could not because at least for now
there is a specification in
In discuss in discussion
But to make it possible, but for now it is not possible to send a body in the get
Method in a request
With the get method you cannot send the body you can only send some things in the url
Meaning in the query stream
Um, but for it for example
Uh what
Why why would you want to send the body to read something?
it
There is a use case famous use case
in
In that if you want to read some results of a big table
And you want some filters
in that table
You will have to send you can send multiple information about
The column that you want in your table about the sorting of any of the column in the table
and then maybe
That for this table you want all this column you want only
Date from
2021 to 2023 and for this column you only want name that begins with
Okay, and still you just want to read data you don't want to post anything you don't want to create a resource
You don't want to modify a resource
Okay
So in this case it's okay
Yes
Yes
For now, but maybe someday we will be able to send the body in the get request
Um, I just want you to know
that
Um
Please please watch my bs code editor
And if I do that and here I just return
Message
The world and I only want
If I only want to search for media and so to call open
Open movie database only when the client requests
Makes a request to media
I want to do this
Okay
I want to create a route handler only for the media route
Okay
And what will happen is maybe not what you expect
meaning that here I
add media
And I click on send request
To 310 okay. Yes
Okay
What in the name
Okay, that is strange that is not what I expected
Sorry
Anyway, we're going to move forward
So that was express express is
Was the de facto standard for
Web applications in node.js
But unfortunately, it has not evolved in the past five or six years
And
It is not really promise ready promise friendly and furthermore it's not really
Performance
And that is why there are numerous
frameworks that
Were built upon HTTP module also
Like fastify and fastify is much more
performance and
Uh is really really similar to express in in its api
Okay
So we are going to take a break maybe five minutes and then we go and refactor our code to use fastify
Okay
See you in five
Or maybe you want more
Five is okay five minutes. Okay. See you in a bit
Oh, yes, here
Okay, everyone's back five minutes is not very long, you know
Okay, so everybody ready
Um
Oh, I am sorry, um, I made you create a git but I
Not make you use it. So maybe what you can do
Is
Uh, sorry, no, no, no, no, no, no, yes, maybe what you can do
Is add all
Oh, no, no, don't do that don't do that. Oh, I know why I didn't please do not do that
Okay, now I really want to talk to you about that so please pay attention yes
Okay, but just a minute it's really important that you know that node modules should not be
Tracked in your git, okay
So you should create a git ignore file and this is what we are going to do and it will take
Oh, I hope I
No, I think I did not so please install the extension which is called git ignore
I don't think I
Get ignored generator from piotr piotr palasz palasz
Palasz I don't know
Yes install it and then
Here if you
If you click on the right in the explorer
No, it's not there okay, sorry so shift control p or f1
Then you go git ignore
Or git ignore would suffice
And then generate git ignore file
Git ignore generator
I think there is just one which is called git ignore this one
From piotr palasz
And then shift control p
And
Then generate git ignore file and then it automatically
Selects for you visual studio code and linux
And please add node and that should do it and then okay
And then you can see that it creates a complete git ignore file for
Armand macos, so
I added it
Here but so some thumbnails of macos and some special
Directories and files that are macos specific
And then here for node it adds all of that and
Here node modules
Okay, because this should not be tracked in your git
Okay
and
And then if you go git status again, you see that I don't have anymore
I don't have the node modules anymore
This is perfect. So now I can git add all
And commit
Dash m
fit
Sparkles
Express server
Okay, please add node in
before
Setting. Okay, please add node. Yes
So now we are going to use fastify
So
We're going to install
fastify with npm i
Fastify we are going to remove express npm remove express
Maybe I should add this. Oh, yes, it is in the course
Okay
Is there any big difference in regard to how the developer should use any of these packages express or fastify?
Yes, you don't want to use express you don't want to use express this is old school this is not performant
Now you can use fastify or restify or nest nest is really complicated in my opinion
Yes, in my opinion, it's
Uselessly complicated really complicated
and
To understand nest you must understand
A whole lot of concepts that I don't have time to talk to you in three days
Under the hood nest uses express by default
And I think this is a mistake and you can use fastify instead but still
Still it's really complicated to use nest
Okay, so
Please just follow along
And you must know what?
What I tell you before you can go to into learning nest
It's really important
Okay, this is the basic and you really need this to understand nest
Okay
So
Here where is my browser
So here we removed express and we installed fastify right
So just like express we import fastify from fastify
And then we create a fastify application with fastify and by calling fastify
Then you add a route handler here
Just like express with fastify.get and then the route and then the handler
And then you run the server by starting to listen on a particular port
So
I don't want you to look at the solution
So now that you have the the hello world in fastify
Try to refactor your code
So you use fastify instead of express. It's really simple. It should be really simple. So please note
That the real difference is differences are this rather true
here
And that you don't need to
Request sorry response.json. You just return an object
Great it works for who? Gold? Stefan. Stefan. Perfect
Yes, and you see
That you have logs and the logs are in json
And they are in one line and this is really important and that's what you want to do
That's what your dev apps will want you to do
It doesn't need to
I don't think it needs to you mean you mean fastify does fastify uses beautify
Yes, but
This is what you want to do in production
maybe
In dev you don't want that and we will have
We will find a way
To make it more readable for us developer
In the dev mode but in production that's what you want to do
You want to json in one line
Sorry for interrupting. I have a question
How come we we don't specify the whole thing because I can see that you only need to specify the port
And it just works
Yes, because by default it runs on localhost
But you can
But you can
You can you can you can specify it
Um, I will show you how
So, um, it's uh, it's good for everyone
Okay, so let me just do it here fastify oh by the way
Um, if I double click on express here it selects the whole world the whole world, okay
This is important to to
To save time, okay, do not do this please
No, just double click on express and it selects express the whole world, okay the whole world
And now you can press on control d
Several times and then everywhere it sees express it selects
The the the next occurrence, okay, and now I can
Transform it to fastify
Okay
Now I can do better than that if I I am here in fastify and then I press f2
I can modify the name of this
um of this variable
to
To make it within a capital f
Okay, and it only
Modifies the the occurrence that are in the same scope
So this is really safe
Okay, this is f2
Okay
Now I want a logger to be true
And then uh
Here the listen
The signature this signature of listen
is
deprecated
So what I have to do is
To make it an object, okay, the first parameter argument should be an object
And host name should be named just host
Okay, and so the first argument here
That I give to app listen is an object
with the port
property with the port number and the host
With the host, okay, this is how you do it. Okay, Martin
You don't have to add this callback because fastify will do it anyway
Npm
Node src
Okay
Here I have the server listening at
Okay, this is done by fastify. I don't have to do anything and this is great. Okay, this is the most
Sensible default and that everyone agrees on okay
That the server should log whenever it's ready and is listening and where and on what port, okay?
so
Now I want to add
Just make sure just make sure it
It works as expected rest.json is not a function. Yes, I forgot
That I just had to return
And also by default express fastify
Fastify sends the 200 status code, okay
If I want to change it
In reply you can make
You can call it status also
Okay, so this is really similar to what express to the api of express. Okay?
Now
This is all good
But I would like to
Instead of just doing that I would like to
Catch if there is an error when I try to start or when the server
crashes
Okay, and I want to exit with an error
process exit zero will
Exit without an error process exit one will
Will exit with an error, okay, this is enforced
Now what can I do
Okay
Maybe now
We can
Several things we can do for now first is that i'm fed up with
Um, um, I don't want to
Cut the server and then restart it manually each time I change anything in the code
I want this to be automatic
Do you know a way to do it?
Maybe we should use the dash dash watch flag instead of notepad
I guess it's available from version 80. Yes, you can do that
Yes, you can do that
Yes, we are going to use notepad and I will tell you why later but yes you can do notepad
Watch, okay
But I want to make it first I want to make it simple and second
We will not use it no directly
In the near future, so I will try to
As soon as possible use a node mon. Okay, so node mon
as the name says monitors
any file or group of file or folders
That may change
and when it detects a change
It automatically
Re-execute the command that you specify to node mon
So here in scripts in the scripts
Property of the package json
What I want to do first is to create a start
A start script that runs node src index.js which means
that instead of
Writing
sr node src.js all I have to do is npm start
And this is really common. Okay, so in production
You want to use just npm start
So that you don't have to know
the I mean the
the person who
runs
the
The ci and the cd and the the devops team
Don't have to know
which
Um file is the entry point
I just want to know that it starts npm start. Okay
That it runs the script npm start
Now for development
I will have a script named dev
And
It will
Launch
node mon
with
src index
and
We want to we want him to
Restart every time either any file of the src folder changes
Or
If the .onc file changes, okay
Since dev is not a standard script like start
We have to use npm run dev
And I have not installed node mon so I will add it but as a
as a
developer
Development dependency so dev dependency to do that. I would add the dash
capital d
After npm install and then node mon and you will see that in package.json. I will have a new property
Called dev dependency. Sorry. It's really too slow here
Okay. Now I can run npm run dev
And
Here you go
It watches
every file
in src and
the .onc file
It is watching the extension
js micadjaxson cgs and json and it starts
node src index.js
Okay, and now if I modify something in
index.js
And then I see here the
The disk here meaning that it's not safe. I'm going to press
ctrl s
And it detects automatically the change
And it restarts index.js from src
This is because we added the watch
Yes, this is because we added
Dash dash watch
Here
These two these two watch is because it becomes that here this line
And if I change the
Up it detects the change and we start index.js
Okay
So this is great now
Uh, what at what time do we end
In five
In 10 minutes, okay, perfect
So i'm just going to tell you about other
registry clients
So now you know about npm npm is one
what we call agent or
npm registry clients
And there are more maybe you know yarn, do you know yarn?
Um, I don't like it much but it has been quite used
Much less now, I think
and
There is another that I really like
Which is called pnpm. I will go back to this in a minute
There is
Another which is bun and bun is not only an a client for npm registry. It's also a new
JavaScript environment
Just like note
And just like dino, maybe you know dino
And dino is
Yes, exactly dino was also created by ryan dal
and
He
He at first he didn't want to have any compatibility with npm but
The the community really wanted it. So now it's compatible and any npm package is compatible with dino
And there is quite a good spot for it
and
And bun is also
a javascript runtime environment and
An npm registry client, okay?
Pnpm why it's my favorite because it supports by default mono repo
Yarn and now npm also supports them
But I think the support of pnpm is much much better
I will I I think I won't have time to talk to you about mono repo, but it's a really good
feature
Pnpm
It reduced reduces this space
How because it uses?
Sim links
And especially for mono repo
You don't download
And the same package twice
unlike npm
So it really saves this space if you have several projects in in one
one
Repository and they all use the same package you won't have
um, you won't have
copies of the same
npm modules
That will just be one and some sim links
And uh and last it's faster than in npm
Okay
There's also another registry which is which is quite new and why and all?
Launched it publicly a few months ago
It's called jsr for javascript registry
And it it looked very promising to me
It is made for typescript and really with typescript in mind and
ECMAScript module
It auto generates docs if there is jsdoc in the
the code
And it auto transpires ts code
This is this is really great
And can also be used in any project with the node modules with any clients
Which may be npm or yarn or pnpm
And can be used in any runtime
Meaning node.js dino van or whatever
So this is really promising for me. It's not quite
It will take time to
To to get traction, I think because npm is really really big but it's really interesting
The package.json file we also
We saw that okay
I will
You can either see my browser or your browser with the course, okay
So the the important properties in package.json are name type version and main
Okay
Main is not really important for you. It's just important for libraries
it's not important for apis and
And frontend it's really important for libraries
The npm scripts we saw start but there is also standard scripts like stop and install and test
And
I talked to you a bit about semware, but I will not talk much about it if you have questions
Don't feel free to to to ask me
And oh, yes, I did not talk about package.log.json
When you run npm.json npm install you created a package.log.json
As the name suggests it blocks every
version of every dependency
Used in your project
And it's really really important that you track it in your in your git
That allows
Every developer on your project to really use the same packages with the same exact version
This is really important
Because if you don't have it
You may use a different version of the same package and so
and and
And so maybe you have bugs that others don't
And maybe you don't have bugs that others do
so
In the same team, you must be sure that you have the same versions of all the package in your
dependency graph
And this is what package.log.json is meant to do
Okay, we saw node man
And
That's it. That's a wrap
Please feel free to ask questions
I hope you
Understood everything. I hope you learned some
Numerous stuff
And
I hope you will be able to understand more and learn more tomorrow
Well really you should
Really use
Logger true. Okay, put every log of your
Please see my vs code
every log
should be
in
You know elk
elastic search
Kibana logstash
elastic search logstash kibana well
every
every log should be
in a database that should be indexed with elastic search and
searchable with kibana
And
And so with that you can monitor every request you make and if you log properly
Things in the your route handlers you must
You will have
Information about what request takes much much much too much time
and
And if you go precisely you can maybe
Get information about some of the requests to the database that are
That take too too too much time
And this is a configuration that we need to do
Or fast if I know that you say this is not that right now we see the terminal
so
what I
what I
what I
Why I told you that you must use
json log
Is that normally an an early player reads all these json?
And knows
Automatically how to transform it to be searchable
Okay
And if you put this in a docker
And this it should be in the docker. I mean in a container
Okay, do you know what container is docker container? Do you know their docker containers?
Well, I hope your clients have a devops team that can
Leverage the power of docker and the containers and the elk
uh the elk stack stack
Uh so that
just by
Logging you have much information needed
and can be
every log is
Saved into a database that is indexed and searchable
If you see my browser, uh, this is french
What language do you want?
So it can
Also make create a graph like this just with the log that you just saw
Well, really this is this is a different
I mean
So you see here's um
They have all the logs and then here
They search something
Okay. This is a key kubernetes labels app kafka and era
Okay, this is search
so
with this search
Appears here only the log that are interesting
For for for him, okay
so
So, um, so as I was saying, this is really a different
Uh a different job. Okay
The this is this does not enter
into the
developer functions
Okay, this is uh devops
Stuff this is not for developers
I know a bit about it. I know a bit about how to use it, but it's not really my job
Okay, and I work with devops and I work with people that really use this and knows how to use this and knows how to
detect some
some uh some errors or some long requests and long queries in sql
and then I I
We talk
Talk together and then I can modify and uh refactor and optimize my code any other questions
On the vue gs is also three days in one month
Okay
Yes, yes and as you can see here in my browser
We did the introduction hello world concepts npm and that's it
We still uh, we still have
All this to do, okay
Well npm we didn't just see npm but
I
I kept it in the npm chapter because
we tried
we used it to create the package json and then to
add some
Some modules from npm and then we used it to
run some scripts
like start and dev
and
We also saw some other registry clients like yan bun pnpm
and another registry
jsr
And so that is why we are still in npm, but we did not just see npm in this chapter. We saw much more
and uh one final question should we commit to the gift
of our current person?
Yes, you should
Uh
No, not for now
But you can you can you can create a github a github for your project and and push it
Yes, you should maybe you should do that if I were you I would do that
Yes, you don't you don't yes exactly you don't have the
The vm afterwards, I think you're welcome. Do you want my?
Code in a github
Okay, okay, I will uh, where where do I
Okay, it will be
Use first
Okay
Yes
Okay, so you can find it in github.com
Here
I put it in the chat. Oh and by the way, you can see that
There are some emojis
in the
my commits
and that was the point of when I
When I did that commit yes, these sparkles here
Creates the emoji that you can see on the second time here
And I installed for you the
The conventional commit extension
And so
If you want to use it it's quite simple it's control control shift p and then you write
Cocoa then you click on convention comics here if you see my vs code
Okay
Control p then
coco
Conventional commits
I click on it
Then it asks if it is a feature a fix or docs
Or whatever then let's say I I create a new feature in that commit
I put no scope. There is no specific scope in in my commit
and then here I
Can put any emoji and I put sparkles because it's a feature
Now I can
create a new
Route for media for example, this is the message commit
And then you can have a longer description if you want if you want details in your commit and this can change
This can have multiple lines the previous one must be in one line
And the the sixth
step
Is really optional
And it should be used really scarcely
rarely rarely
If if it is if the api
Has this commit introduces breaking changes in the api and then you go
Breaking I think this is uh, I don't know. Maybe it's
The predicate
Move
And you see here that I have feet colon and then the emoji and then the comet and then
the the breaking change
So this this would be as I was saying
So conventional commit is a
You see my browser now
it is a standard way of
Uh for the message commit commit message, sorry
Okay, and the emoji is completely optional. I really like it, but it's completely optional
And this is the way to create breaking chain
This is another way with the exclamation mark here this tells that
It has breaking changes and this can be used
Oh, yes, really important
Why do I love conventional commits it is because this can be used to automatically generate
change logs
A change log
Uh built automatically can
Read all the commits from the last release to the the last commit
And
List every fix list every fit feature a new feature and and list every breaking change and create it
And create a text file
Um that you can add to the changelog and this changelog can be modified also automatically
There are several ways to do it. I use semantic release
But there are other ways
You can you can you can find changelog generators on the internet this go way beyond the
the course, okay, but
I was just trying to
To answer the question
Okay
Well, see you tomorrow 10 o'clock for you nine for me
Okay
Okay, thank you. Thank you very much. Thank you. Thank you. Bye. Thank you
Bye                

on 2024-07-08

Visit the Node.js course recordings page