Middleware a cikin Express.js: Gudanar da Buƙatun Matsakaici

Gabatarwa Middleware zuwa Express.js

Middleware in Express.js shine ra'ayi mai ƙarfi wanda ke ba ku damar aiwatar da ayyuka a cikin takamaiman tsari yayin zagayowar amsawar buƙata. Waɗannan ayyuka na iya yin ayyuka daban-daban kamar tantancewa, shiga, tabbatar da bayanai, da ƙari. Middleware ana aiwatar da ayyuka a jere, kuma kowanne middleware yana da damar yin amfani da abubuwa request da response abubuwa, da kuma next aikin, wanda ke ba da iko zuwa na gaba middleware a cikin tari.

Me yasa Amfani Middleware ?

Middleware yana da mahimmanci don daidaita ayyukan aikace-aikacen ku da haɓaka ƙarfinsa. Yana ba ku damar kiyaye masu kula da hanyoyinku da tsabta da mai da hankali kan takamaiman ayyuka yayin sauke abubuwan gama-gari ko ratsawa ga middleware ayyuka. Wannan rarrabuwar damuwa yana haɓaka sake amfani da lambar kuma yana sa tushen lambar ku ya fi tsari.

Ƙirƙira da Amfani Middleware

Don ƙirƙira middleware a cikin Express.js, kuna ayyana aiki mai ɗaukar sigogi uku: request, response, da next.

Anan ga ainihin misalin middleware wancan rajistan ayyukan kowane buƙatun mai shigowa:

const logMiddleware =(req, res, next) => {  
  console.log(`Received a ${req.method} request at ${req.url}`);  
  next(); // Pass control to the next middleware  
};  
  
app.use(logMiddleware);  

Kuna iya amfani da app.use() hanyar don amfani middleware a duniya zuwa duk hanyoyi, ko za ku iya amfani da shi zaɓi don takamaiman hanyoyi.

Middleware Odar Kisa

Middleware ana aiwatar da ayyuka a cikin tsari da aka ayyana su ta amfani da app.use().

Misali:

app.use(middleware1);  
app.use(middleware2);  

A wannan yanayin, middleware1 za a aiwatar da shi kafin middleware2 duk buƙatun masu shigowa.

Magance Kurakurai a ciki Middleware

Idan kuskure ya faru a cikin middleware aiki, zaku iya wuce kus ɗin zuwa next aikin, kuma Express.js za ta tsallake ta atomatik zuwa sarrafa kuskure middleware.

Ga misali:

const errorMiddleware =(err, req, res, next) => {  
  console.error(err);  
  res.status(500).send('Something went wrong!');  
};  
  
app.use(errorMiddleware);  

Amfani Middleware don Tabbatarwa

Middleware yawanci ana amfani dashi don aiwatar da tabbaci da izini a aikace-aikacen yanar gizo. Misali, zaku iya ƙirƙira middleware aikin da zai bincika ko mai amfani yana da ingantacciyar hanya kafin ba da izinin shiga wasu hanyoyi:

const authenticateMiddleware =(req, res, next) => {  
  if(req.isAuthenticated()) {  
    return next(); // User is authenticated, proceed to the next middleware  
  }  
  res.redirect('/login'); // User is not authenticated, redirect to login page  
};  
  
app.get('/profile', authenticateMiddleware,(req, res) => {  
  res.send('Welcome to your profile!');  
});  

 

Kammalawa

Middleware in Express.js shine kayan aiki mai mahimmanci don sarrafawa da haɓaka ayyukan aikace-aikacen yanar gizon ku. Ta hanyar ƙirƙira middleware ayyukan sake amfani da su, zaku iya daidaita lambar ku, daidaita abubuwan damuwa, da haɓaka haɓakar ayyukanku gaba ɗaya. Daga sarrafa ingantacciyar hanyar shiga da sarrafa kuskure, middleware yana ba ku ikon gina ƙaƙƙarfan aikace-aikacen gidan yanar gizo da inganci.