ExpressJs

⌘K
  1. Home
  2. Docs
  3. ExpressJs
  4. মিডলওয়্যার

মিডলওয়্যার

মিডলওয়্যার আসলে কি ?

মিডলওয়্যার কি তা তার নামের মধ্যেই আছে । মিডলওয়্যার রিকুয়েস্ট ও রেস্পন্সের মধ্যে সম্পর্ক স্থাপনকারী । ইউজার এর HTTP রিকুয়েস্ট ফিল্টার করে রিকুয়েস্টের পরে রেস্পন্স কি হবে তা মিডলওয়্যারে লেখা থাকে।

কখন মিডলওয়্যার ব্যবহার করব ?

১. যেকোনো রকমের ইউজার অথেনটিকেশন ।

২. রাউটের মাধ্যমে আসা কোন API KEY হ্যান্ডেল করতে ।

৩. আপনার অ্যাপের জন্য এক্সেস মডিফায়ার তৈরি করতে (যেমনঃ ইউজার গেস্ট হলে কি দেখবে অথবা ইউজার অ্যাডমিন হলে কোন পেজ দেখাবে) ।

মিডলওয়্যার ২ ধরণের

  • Application middleware
  • Route middleware

Application middleware এবং route middleware-এর মধ্যে পার্থক্য

বৈশিষ্ট্যApplication middlewareRoute middleware
প্রয়োগের পরিধিসমস্ত request-response চক্রনির্দিষ্ট route-এর request-response চক্র
উদ্দেশ্যapplication-এর সামগ্রিক কার্যকারিতা উন্নত করাroute-এর কর্মক্ষমতা বা কার্যকারিতা উন্নত করা
উদাহরণlogging, error handling, caching, securityauthentication, authorization, data validation, response formatting

Application Middleware এবং Route Middleware এর উদাহরণ

  1. Application Middleware: এটি সম্পূর্ণ অ্যাপ্লিকেশন জুড়ে প্রযোজ্য।
  2. Route Middleware: এটি নির্দিষ্ট রুটের জন্য প্রযোজ্য।

উদাহরণ কোড

1. Application Middleware

এটি সম্পূর্ণ অ্যাপ্লিকেশন জুড়ে লগিং করে এবং প্রতিটি রিকোয়েস্টের জন্য সময় লগ করে।

const express = require('express');
const app = express();
const port = 3000;

// Application Middleware - Logger
app.use((req, res, next) => {
  const currentTime = new Date().toISOString();
  console.log(`[${currentTime}] ${req.method} ${req.url}`);
  next(); // পরবর্তী middleware ফাংশনে নিয়ন্ত্রণ পাস করে
});

// Root Route
app.get('/', (req, res) => {
  res.send('Hello, World!');
});

// About Route
app.get('/about', (req, res) => {
  res.send('About Page');
});

// Start the server
app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`);
});

এই উদাহরণে, প্রতিটি রিকোয়েস্টের জন্য লগিং তথ্য কনসোলে প্রদর্শিত হবে।

2. Route Middleware

এটি নির্দিষ্ট রুটের জন্য অথেনটিকেশন চেক করে।

const express = require('express');
const app = express();
const port = 3000;

// Dummy authentication function
const isAuthenticated = (req, res, next) => {
  // এখানে একটি সাধারণ অথেনটিকেশন চেক (শুধু উদাহরণের জন্য)
  if (req.headers['authorization'] === 'Bearer token123') {
    next(); // অথেনটিকেশন সফল হলে পরবর্তী middleware ফাংশনে নিয়ন্ত্রণ পাস করে
  } else {
    res.status(403).send('Forbidden');
  }
};

// Application Middleware - Logger
app.use((req, res, next) => {
  const currentTime = new Date().toISOString();
  console.log(`[${currentTime}] ${req.method} ${req.url}`);
  next();
});

// Public Route
app.get('/', (req, res) => {
  res.send('Hello, World!');
});

// Protected Route with Route Middleware
app.get('/dashboard', isAuthenticated, (req, res) => {
  res.send('Welcome to the Dashboard');
});

// About Route
app.get('/about', (req, res) => {
  res.send('About Page');
});

// Start the server
app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`);
});

এই উদাহরণে, /dashboard রুটটি অথেনটিকেশন চেকের মাধ্যমে রক্ষিত হয়। isAuthenticated middleware ফাংশনটি শুধুমাত্র ঐ রুটের জন্য প্রযোজ্য।

রান এবং টেস্ট

  1. Application Middleware টেস্ট করা:
    • টার্মিনালে অ্যাপ্লিকেশন চালু করুন: node server.js
    • ব্রাউজারে বা curl কমান্ড দিয়ে বিভিন্ন রুট অ্যাক্সেস করুন:
curl http://localhost:3000/
curl http://localhost:3000/about

আপনি কনসোলে লগ দেখবেন:

[2024-08-03T12:34:56.789Z] GET /
[2024-08-03T12:35:00.123Z] GET /about

Route Middleware টেস্ট করা:

  • টার্মিনালে অ্যাপ্লিকেশন চালু করুন: node server.js
  • ব্রাউজারে বা curl কমান্ড দিয়ে প্রোটেক্টেড রুট অ্যাক্সেস করুন:
curl http://localhost:3000/dashboard -H "Authorization: Bearer token123"
curl http://localhost:3000/dashboard

সফল অথেনটিকেশন:

curl http://localhost:3000/dashboard -H "Authorization: Bearer token123"<br>

রেসপন্স:

Welcome to the Dashboard

How can we help?