What is Middleware in Express JS? | Node.js Tutorials for Beginners

  Рет қаралды 38,993

Dave Gray

Dave Gray

Күн бұрын

Web Dev Roadmap for Beginners (Free!): bit.ly/DaveGrayWebDevRoadmap
What is middleware in Express JS? You will learn the answer in this Node.js & Express tutorial for Beginners. Error handler and logger custom middleware, built-in middleware and third party middleware will all be applied in this lesson for beginners.
🚩 Subscribe ➜ bit.ly/3nGHmNn
🚀 This lesson is part of an Node.js & Express for Beginners tutorial series playlist:
• Node.js Tutorials for ...
🔗 Starter Source Code: github.com/gitdagray/express_...
What is Middleware in Express JS? | Node.js Tutorials for Beginners
(00:00) Intro
(00:05) Welcome
(00:15) Intro and Setup
(00:33) What is middleware?
(01:37) Applying built-in middleware
(07:03) Creating custom middleware
(14:49) Adding third party middleware
(23:00) Creating a custom error handler
(30:43) app.use() vs app.all()
(32:03) Improving the custom 404 route handler
📚 References:
Node.js Official site: nodejs.org
NPM Official site: www.npmjs.com/
Express JS Official site: expressjs.com/
MDN CORS: developer.mozilla.org/en-US/d...
NPM CORS: www.npmjs.com/package/cors
✅ Follow Me:
Twitter: / yesdavidgray
LinkedIn: / davidagray
Blog: yesdavidgray.com
Reddit: / daveoneleven
Was this tutorial about middleware in Express JS helpful? If so, please share. Let me know your thoughts in the comments.
#express #middleware #beginners

Пікірлер: 72
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
This tutorial will help you learn all about middleware in Express JS. It applies built-in middleware, custom middleware that we will construct to log events and handle errors, and third party middleware. If you're just getting started with Node.js, you may want to start at the beginning of the Node.js for Beginners tutorials playlist found here: kzbin.info/aero/PL0Zuz27SZ-6PFkIxaJ6Xx_X46avTM1aYw
@Alibeee87
@Alibeee87 2 жыл бұрын
Thank you for all your help! I appreciate you mentioning keyboard commands as well, that's fantastic info for a beginner!
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
You're welcome! 🙏
@wgllgw
@wgllgw Ай бұрын
Hi Dave, you must be a very good teacher/mentor. I would love to work under/learn from someone like you! You addressed a lot of pertinent questions, in a prudent sequence. Plus your explanations are clear and concise. It lends a lot of motivation to why things are they way they are (these are the hardest thing to Google!). Thank you so much for taking the time and effort to share your knowledge and wisdom!
@viciousinsights6902
@viciousinsights6902 2 жыл бұрын
This has been truly enlightening, this series has helped me tremendously with the aspects of node & expressjs I had been struggling with. I look forward to learning more from you in the future!
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Glad it helped! 🚀
@ampinkus
@ampinkus 7 ай бұрын
Hi Dave, I just finished this module of the course and o far it is really excellent! I would only recommend that each module should start with an introductory slide explaining what will be developed in the module and perhaps a graph with the interaction of the different functions that will be implemented.
@pets_talk
@pets_talk 2 жыл бұрын
great tutorial! Coming from the frontend world, it really helps me to undestand how the backend works!
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Happy to help! Thanks Natalia! 🙏💯
@juangomez874
@juangomez874 2 жыл бұрын
Dave, you are my new hero. Thanks a lot for these videos, they had helped me a lot. Very very clear. Keep up the good job.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Thank you for the kind words, Juan! 🙏
@MrQVeeBoo
@MrQVeeBoo Жыл бұрын
Very nice explanation. Step by step. Easy to follow. Thx
@ofofononoumoren
@ofofononoumoren 11 ай бұрын
Absolutely great video. Just for improvement purpose, it's best to import logger as a single value (const logger = require('./destination')) rather than as an object with named properties since that not how it was exported in the logEvents file. Even if, it doesn't matter anyways. Thanks for this very elaborate module, Dave.
@cyberpaimpol
@cyberpaimpol Жыл бұрын
Super tutorial, progression is smooth and so interesting, thx !
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Happy to hear that!
@saquibansari7866
@saquibansari7866 Жыл бұрын
6:02 Check this time stamp And can you please elaborate that the path of css file you have assigned is different as we used to assign without middlware. I found it after searching alot that the path of middlware and the path of opening html file in a browser will be different. I still don't know why it is so. So kindly clear this out.. please please please please❤❤❤❤❤
@NaviaryMusic
@NaviaryMusic Жыл бұрын
Thanks Dave!! My websites coming along nicely!
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Fantastic!
@soudaminipanda
@soudaminipanda Жыл бұрын
Very nice tutorial. Very easy to understand
@loikvokhidov7688
@loikvokhidov7688 Жыл бұрын
algorithm should recommend these videos to all the people who want to learn NodeJS.
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Thank you!
@ahmad-murery
@ahmad-murery 2 жыл бұрын
Well explained Dave, Middleware can be very handy to allow only authentic users to access certain pages, I recently started using the Rest Client extension for VS Code instead of Postman, I found it easier to work with as I don't have to leave VS Code to test my API, it also let me have all my requests in a plain text file that can be included side by side with the app files. Thanks Dave 🚀
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Thank you Ahmad. 🙏 Yes, there are many good applications of middleware including authentication. Is the VS Code extension you are using called Thunder Client? I have heard good things!
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
@@ahmad-murery thank you my friend! I will check them both out and compare. Have a great weekend!
@ahmad-murery
@ahmad-murery 2 жыл бұрын
@@DaveGrayTeachesCode Have a great day you too my friend, if possible, make a video about the comparison and maybe put it under the web dev tools playlist, regards
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
@@ahmad-murery thank you - great idea, Ahmad!
@baxtables
@baxtables Жыл бұрын
when a new Error("Not allowed by CORS") is created, does the app.use(errorHandler) receive it automatically, because it has a first arguement as err?
@keithbacalso9433
@keithbacalso9433 2 жыл бұрын
Even I did not removed the `.css` I can still access it in the browser.... ? why?
@DrCash7
@DrCash7 10 ай бұрын
Ona managed vps and keep running into cors error when my front end loads and talks to the backend. I've added the cors settings after the installed apps and still running into cors errors. Need halp
@unique3345
@unique3345 Жыл бұрын
A very useful tutorial!!!!
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Glad it was helpful!
@Pareshbpatel
@Pareshbpatel 9 ай бұрын
A comprehensive tutorial on Middleware in Express, beautifully presented. Thanks, Dave {2023-10-05}: implemented
@Uniqlo921
@Uniqlo921 3 ай бұрын
Hey dave, at @34:51, I accidentally forgot the "else" in the if else block for req.accepts('json'). So it was if(req.accepts('html') followed by if(req.accepts('json'). When I tested with my browser, I got the JSON response. So it seems that my request accepted both html and json. However, I noticed that you said we needed postman to test the json functionality, which I agreed with until I hit my issue. Do you know why the above happened. I also wanted to thank you for making such a fantastic and comprehensive tutorial on Node. I am currently using this to get out of my automated testing pigeonhole :)
@piotrekjazz1287
@piotrekjazz1287 2 жыл бұрын
Apart from the great content, thanks a lot also for the source code, that slowly becomes a youtube standard, at least at most of the popular channels.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
You're welcome. 🙏 Early on in this series, I don't think I provided it. I often think typing out the code helps students learn. As we started to put together a REST API project I thought the source code would be good to build upon as the series progressed.
@piotrekjazz1287
@piotrekjazz1287 2 жыл бұрын
@@DaveGrayTeachesCode Good point, but most of the time you want to check/test just a small bits and then it is problematic writing everything.
@moiraleon8744
@moiraleon8744 2 жыл бұрын
Quick question, what extension do you have that allows your file names in your Explorer tab to have different icons and colors?
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
That is vscode-icons. I recommend 5 extensions here: kzbin.info/www/bejne/roLGoYScetqgh9k
@tabliqatchi6696
@tabliqatchi6696 2 жыл бұрын
This is gem people!
@sonamohialdin3376
@sonamohialdin3376 2 жыл бұрын
Amazing tutorial thank you
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
You're welcome, Sona! 💯
@denmccormik7654
@denmccormik7654 Жыл бұрын
Thank you for the lesson. Do you know why browser doesn't allow to fetch from console from it's starting window (when url is empty, when we just opened the browser without starter page)? I thought that origin just will be undefined but everything would work.
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
The browser always implements cors. Ref: developer.mozilla.org/en-US/docs/Web/HTTP/CORS
@ramziosta
@ramziosta 2 жыл бұрын
I got an error optionsSuccessStatus undefined , so I declared let optionsSuccessStatus; outside corsOptions, and that removed the error and got everything to work fine. is that an allowed hack?
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
I would not do that. I suggest downloading my source code and comparing to yours to find the difference and what caused your error.
@MohammadBarghamadii
@MohammadBarghamadii Жыл бұрын
Thank you so much
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
You're welcome, Mohammad!
@brandonjoaocastillo7490
@brandonjoaocastillo7490 Жыл бұрын
Why you dont use next() in the errorHandler function? you did it in logger, its because is the last middleware or something like that?
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
You are correct - if my memory is right, it is last.
@bnmbnm7278
@bnmbnm7278 2 жыл бұрын
24:56 Excuse me, can you explain why the code(line 80-83) work? How does it work? I cannot understand...Thank you...
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
app.use() indicates we are going to use middleware. We're passing in an anonymous function as the middleware. The function is sending an http status code of 500 along with the error message in the response to the client.
@sdfsdfsdffdsfsdfsd123
@sdfsdfsdffdsfsdfsd123 2 жыл бұрын
thank you.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Welcome!
@DeepakGupta-hj2dv
@DeepakGupta-hj2dv 2 жыл бұрын
Can you please make a video node JS microservices crash course
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Thank you for the good suggestion! 🙏
@kkomax7
@kkomax7 10 ай бұрын
This tut hasn't been liked enough. I usually don't comment to thank, but thanks Dave
@alirezahekmati7632
@alirezahekmati7632 2 жыл бұрын
🚀
@vigneshb5717
@vigneshb5717 Жыл бұрын
so far ok completed the second video
@NoorNoor-bl2jr
@NoorNoor-bl2jr Ай бұрын
There is no logs file in your git repository.
@DaveGrayTeachesCode
@DaveGrayTeachesCode Ай бұрын
Yes, you should create your own logs.
@AuxiliaryOfficial
@AuxiliaryOfficial Жыл бұрын
im looking for your typescript tutorial
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
It is coming soon! 🚀
@AuxiliaryOfficial
@AuxiliaryOfficial Жыл бұрын
@@DaveGrayTeachesCode really thanks, typescript + react 😍
@shineLouisShine
@shineLouisShine Жыл бұрын
Thank you very much for this lesson. With all due respect. and I do sincerely and deeply have A LOT of respect for everything you are doing here: For me, the last part where you immediately say to "Like" the video is a bit too aggressive, Without a single second of silence to breathe, to process, and to execute the "Like" on our own - Without you asking us to do so, but just because of a natural sense of gratitude. I feel less sympathy for this kind of... "strategy"(?), because... Whoever likes the video - will give them a "Like", And whoever likes to subscribe - will certainly subscribe. Furthermore, think of people who already subscribed to your channel, And yet don't have any other choice but to listen to this request in the end of each and every video, Regardless of the fact that they already did it. My suggestion (/request) : Give your viewers a little bit more credit 😌 (in addition, there is actually a graphic invitation to subscribe during the videos already...)
@Jazz__Dev
@Jazz__Dev Жыл бұрын
is channel is not the only one you know... As a matter of fact, I love listening to that part... just watch the tutorial and move on... All these are absolutely unnecessary..
@Jazz__Dev
@Jazz__Dev Жыл бұрын
fetch('localhost:3500') Promise {} VM1113:1 GET localhost:3500/ net::ERR_FAILED (anonymous) @ VM1113:1 VM1113:1 Uncaught (in promise) TypeError: Failed to fetch at :1:1 HI Dave, not sure if i'm the only one getting this particular error and i'm not even sure where to fix it... Why is the console fetch throwing that error please?
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
The fetch is failing, but it is difficult to say why from here. After it fails, you get a type error. Go back over it and check your source code compared to mine. One little thing can make a difference.
@AdityaDey424
@AdityaDey424 Жыл бұрын
Hey have you solve the issue? I also get this problem. If you solve this then please told me the solution.
@kaustavbhattacherjee7952
@kaustavbhattacherjee7952 Жыл бұрын
I'm getting the same error as well even when I use the exact same snippet
@shreyalanka
@shreyalanka 7 ай бұрын
Same here😢
@aseemgandhi2867
@aseemgandhi2867 4 ай бұрын
Same but i am also not getting data in log file when i used fetch('localhost:3500') in google.com
Learn Express Middleware In 14 Minutes
14:48
Web Dev Simplified
Рет қаралды 390 М.
아이스크림으로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 59 МЛН
KINDNESS ALWAYS COME BACK
00:59
dednahype
Рет қаралды 162 МЛН
NPM Node Package Manager Modules | NPM Tutorial for Beginners
23:23
What is Middleware?
10:11
IBM Technology
Рет қаралды 91 М.
What is Express middleware and how does it work? (in-depth)
30:28
Full Stack Zach
Рет қаралды 60 М.
Learn Express JS In 35 Minutes
36:03
Web Dev Simplified
Рет қаралды 775 М.
Intro to MongoDB and Mongoose | Node.js Tutorials for Beginners
17:06
Express JS #9 - Middleware
21:42
Anson the Developer
Рет қаралды 4,8 М.
S24 Ultra and IPhone 14 Pro Max telephoto shooting comparison #shorts
0:15
Photographer Army
Рет қаралды 8 МЛН
САМЫЙ ДОРОГОЙ ЧЕХОЛ! В стиле Mac Pro
0:35
Choose a phone for your mom
0:20
ChooseGift
Рет қаралды 7 МЛН
$1 vs $100,000 Slow Motion Camera!
0:44
Hafu Go
Рет қаралды 25 МЛН
Как бесплатно замутить игровой ноутбук
1:00
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 238 М.