Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
So, after all the talk about middleware,
let's now actually go ahead and
create our own middleware functions.
And again, remember that we actually
already used middleware before.
So let's take a look at that.
And, somewhere up here.
And so, you see that in order
to use middleware, we used app dot use, okay?
So, the use method is the one that we use in order to,
yeah, actually use middleware.
So, add middleware to our middleware stack, okay?
So this express dot json here calling this json method
basically returns a function, okay?
And so that function is then added to the middleware stack.
And so, similar to that,
we can create our own middleware function.
So, let's do that now.
And so, of course, we still need to use app dot use.
Okay, and so now, in here, all we have to do is to path
in our function that we want to add to the middleware stack.
So, remember from the last video that,
of course, in each middleware function,
we have access to the request and the response, okay?
But also, we have the next function.
And so, just like this, we edit as a third argument
to this middleware function, okay?
And like this, express then knows
that we are actually defining a middleware here.
Alright.
Now, just like before, actually,
we could have called this argument here something else,
like X, or N, or, it doesn't really matter.
What matters is that it's the
third argument to this function.
So express basically passes the next function
as the third argument into this middleware function.
And we can then call it whatever we want.
But again, next is really the convention in express,
and in order to avoid confusion,
we always use this name, okay?
And the same for request and response,
as I mentioned before, we could call them something else.
But the convention is to call them like this.
Anyway, let's just log something to the console,
here in this middleware function,
just so that we have some code to actually run
each time there is a new request.
Let's say hello
from the middleware.
And add some emoji here, again, to make it pop a little bit.
I really like that.
And what happens here?
Ah, right.
So that is the code that we want to execute here.
And now, just as we talked in the last video,
we actually need to call the next function, okay?
And if we didn't call next here,
well, then the request/response cycle
would really be stuck at this point.
We wouldn't be able to move on,
and we would never ever send back a response to the client.
So I can't stress enough how important it is
to never forget to use next in all of your middleware.
Okay, and it's as simple as that.
All we have to do is to specify next.
So, this argument here.
And then actually call that function like this.
Okay.
And with that, we are actually ready to test it out.
And all we have to do is
to send a simple request to our API.
So, here is the API.
Let's close up this one here.
And so, it doesn't really matter, because this,
of course, applies to every single request.
So, let's go back.
And indeed, we have hello from the middleware
logged to our console, okay?
So, great.
And I wanted to quickly touch on what I just said before,
which is that this middleware here applies
to each and every single request, okay?
And that's because we didn't specify any route.
So, remember that before I said that all route handlers
here are actually kind of middleware themselves.
They are simply middleware functions
that only apply for a certain URL.
So a certain route, okay.
But these more simple middleware functions
that we define up here,
well, they are going to apply to every single request.
At least, if the route handler comes
before this middleware.
So let me actually show you something.
And I'm going to cut it from here and
now actually put it here
after this route handler.
So what do you think is going to happen now
when I make a call to this route?
So let's see what happens.
So this exact route, so the one that I just showed you,
let me send the request to that.
And now let's go back and now we don't have
hello from the middleware.
So, why is that?
Well, simply because this middleware,
so this route handler here,
it comes before this middleware function that we have here.
And this route handler, which in this case,
is get all tours, actually ends the request response cycle.
So lets take a look at that.
So we have get all tours.
So, where is that?
Yeah, so here is the function and by sending a result
with res dot json, we actually
end the request response cycle.
And so the next middleware in the stack,
which in this case, is
is this one, so our custom one will then not be called.
Again, because the cycle has already finished, okay.
So make sure to understand that this order
really matters a lot in express, okay.
It's fundamental to understand
that this is how express apps work, okay.
So, in order to kind of test that,
let's try to see what happens when
we do a request to this route.
So to get tour update tour or delete tour.
So lets do this one, send the request here,
and let's go back and now we have
hello from the middleware.
So that is actually what we expected, right?
So that is because of course, this middleware here
now is before the route handler.
And so it is, of course, part of the middleware stack
that get executed before the request response cycle ends.
Alright, make sense?
So, let's take that back and so usually
we define this kind of global middleware
here before all our route handlers.
Alright, so this is one very simple
middleware function that we just defined to run some code.
But lets actually do another one.
And of course, we can have as many
middleware functions as we like.
And this time we actually want to
manipulate the request object.
So the signature here is always the same,
always request, response, and next.
And now let's actually manipulate the request.
All we want to do in this case,
is to add the current time to the request.
So we can simply define a property on the
request object called request time.
And then set it to a
new date,
which basically translates to right now.
And then we can use a very handy date function,
which is called to ISO string,
which will then convert it into a nice,
readable string for us.
So, let's pretend we have some route handler
that really needs the information about
when exactly the request happens.
And so the very simple solution is to simply add
something like this to a request using middleware.
Alright?
Now, don't forget, of course,
to call the next middleware in the stack, okay.
So right now, we have request, time on all requests.
So we can now use some route handler here,
for example, (mumbles) for getting all the tours
to simply log that for us to the console.
So console dot log, and its actually request time.
Okay, or we could even sent this in the response as well.
Let me just test that.
And let's just call this one requested at
and req dot request time.
That's the one.
And so, yeah, let's go ahead and figure this one out.
So our middleware is correct, right?
Yeah, so we call next, when we're done,
and so let's test this now.
Remember it's on get all tours.
And when we go back now, or when we go up,
well, it should actually be here.
So, what is wrong here?
You see that down here,
we actually just logged a function and yeah,
of course, we didn't actually call the two ISO strings.
So it's a method we have to call it.
So give it a save, try it again.
And now we have requested at and then today's date.
So, perfect.
That came from our middleware.
So simply because we added that property to our request.
Great.
So I hope that with that, you made a great step forward
in order to really understand how Node app work,
how middleware works,
how the entire request response cycle works,
because that really is going to make all the difference
when you're writing your own applications.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.