Learn Express.js: Middleware Functions + Mario Bros.👨‍🔧🍄 🐢

Where do these pipes go? 🧐 🤔 🤨
Hey there fellow reader,If you plan on reading this blog, you’ll need to know a few things first. I encourage you to read some of my blogs further explaining these topics. It’s totally worth your time! Remember understanding the Basics and the lingo is key. Don’t sweat the small stuff.Here’s the rub…- Basics Node.js- Basic understanding of how Servers/back-end works- Basic understanding Request/Response Cycles- The concept of Abstraction(Computer Science)- Basic understanding of Frameworks- Basic Understanding of Javascript Syntax (ES6)- FYI I like to use Mac OS + VSCODE,but feel free to use what you like !
Confused or Need a refresher? Give these links a try! 🙋‍♀️ 🙋‍♂️ 🙋Node.js: https://ineedmoreplates.medium.com/learning-node-js-8288e0411957Setting up a Node Server: https://ineedmoreplates.medium.com/node-js-setting-up-a-server-534d86658252Express.js: https://ineedmoreplates.medium.com/express-js-learn-with-p-e-memes-bfa080b5a918
This is not Middleware 🐢
“Middleware is software that bridges gaps between other applications, tools, and databases in order to provide unified services to users. It is commonly characterized as the glue that connects different software platforms and devices together.”- talend
😯 Umm… never knew you could that 😳
You remember, right ? 🤓…import and declare stuff here…const server = http.createServer((res,req) => {“do stuff here with res or req”})… listen for port here…
Vanilla Node.js 🤔…in ./app.jsconst http = require(‘http’);const routes = require(‘./routes’)const server = http.createServer(routes.handler);server.listen(3000); — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -To something like this…Node.js + Express.js 🤩…in ./app.jsconst http = require(‘http’);const express = require(‘express’); ←- changed 😮const app = express(); ←- changed 😮const server = http.createServer(app); ←- changed 😮server.listen(3000);*** It get’s more complex then this… ***
About Middleware(3rd party) + Middleware PatternThe Middleware Pattern allows us to ALSO use third party middleware. Which we dicussed earlier. Try not to confuse this with the concept of middleware functions/middleware! 😝
Hurray Tutorials!!!! 🐢 😀 😎
…still in ./app.jsconst http = require(‘http’);const express = require(‘express’);const app = express(); ← imported + invoked 🤨const server = http.createServer(app); ← We need to refactor 💡server.listen(3000);
const http = require(‘http’);const express = require(‘express’);const app = express();app.use(() => { }); ← .use() method takes a function 🤔const server = http.createServer(app);server.listen(3000);
const http = require(‘http’);const express = require(‘express’);const app = express();app.use((req,res,next) => {}); ← takes 3 arguements 🧐const server = http.createServer(app);server.listen(3000);
Don’t shy away from app.use()😂
app.use((req,res,next) => {console.log(‘First Middleware Function!!!’) ←- 😮});
const http = require(‘http’);const express = require(‘express’);const app = express();app.use((req,res,next) => {console.log(‘First Middleware Function!!!’)next(); ←- use next() 👍});app.use((req,res,next) => {console.log(‘Next Middleware Function!!!’)});const server = http.createServer(app);server.listen(3000);
const http = require(‘http’);const express = require(‘express’);const app = express();… MIDDLE WARE 1st, I run first … ️🐇… MIDDLEWARE 2nd, I run second … 🐢const server = http.createServer(app);server.listen(3000);
const http = require(‘http’);const express = require(‘express’);const app = express();app.use((req,res,next) => {console.log(‘First Middleware Function!!!’)next();});app.use((req,res,next) => {console.log(‘Next Middleware Function!!!’)});const server = http.createServer(app);server.listen(3000);
Middleware Functions: https://expressjs.com/en/guide/using-middleware.htmlOn middleware(talend): https://www.talend.com/resources/what-is-middleware/#:~:text=Middleware%20is%20software%20that%20bridges,software%20platforms%20and%20devices%20together.

Career Changer, Software Engineer & Web Developer