Sarrafa bayanan shigarwa a ciki Express.js

Lokacin gina aikace-aikacen yanar gizo, sarrafa bayanan shigar da mai amfani abu ne mai mahimmanci don ƙirƙirar fasali masu ma'amala da sassauƙa. A cikin Express.js yanayin haɓakawa, zaku iya sarrafa bayanan shigarwa cikin sauƙi daga fom da buƙatun HTTP daban-daban kamar GET, POST, PUT, PATCH, da DELETE. Anan ga cikakken jagora tare da hanyoyi da misalai da yawa don taimaka muku cimma wannan:

Karbar Bayani daga Form

Ƙirƙirar HTML Form: Fara da ƙirƙirar HTML form a cikin Pug ko fayil EJS. Tabbatar cewa kun saita action sifa a cikin <form> alamar don tantance hanyar da za a aika buƙatar.

<form action="/process" method="post">  
  <input type="text" name="username" placeholder="Username">  
  <input type="password" name="password" placeholder="Password">  
  <button type="submit">Submit</button>  
</form>  

Buƙatar Gudanarwa POST: A cikin mai sarrafa hanya, yi amfani da body-parser tsakiyar kayan aiki don cire bayanai daga POST buƙatar.

const bodyParser = require('body-parser');  
  
app.use(bodyParser.urlencoded({ extended: true }));  
  
app.post('/process',(req, res) => {  
  const username = req.body.username;  
  const password = req.body.password;  
  // Process data and return results  
});  

 

Gudanar da Nau'o'in Buƙatun Daban-daban tare da Misalin Shiga

Aika POST Buƙatun daga Login Form: A cikin HTML form, tabbatar da cewa kun saita post hanya da action sifa don ƙayyade hanyar da POST za a aika buƙatar.

<form action="/login" method="post">  
  <input type="text" name="username" placeholder="Username">  
  <input type="password" name="password" placeholder="Password">  
  <button type="submit">Login</button>  
</form>  

Buƙatar Buƙatar POST Shiga: A cikin mai sarrafa hanya, yi amfani da body-parser matsakaicin kayan aiki don cire bayanai daga POST buƙatun da aiwatar da aikin shiga.

const bodyParser = require('body-parser');  
  
app.use(bodyParser.urlencoded({ extended: true }));  
  
app.post('/login',(req, res) => {  
  const username = req.body.username;  
  const password = req.body.password;  
  
  // Check login information  
  if(username === 'admin' && password === '123') {  
    res.send('Login successful!');  
  } else {  
    res.send('Login failed!');  
  }  
});  

 

Gudanarwa PUT da DELETE Buƙatun

Buƙatun PUT Karɓa: Don sarrafa PUT buƙatun, zaku iya amfani da hanya da tsaka-tsaki don cire bayanai daga buƙatun da aiwatar da sabuntawa daidai.

app.put('/update/:id',(req, res) => {  
  const id = req.params.id;  
  const updatedData = req.body;  
  // Perform data update with corresponding ID  
});  

Buƙatun Karɓa DELETE: Don karɓar DELETE buƙatun, kuma yi amfani da hanya da kayan tsakiya don gano ID da yin gogewa.

app.delete('/delete/:id',(req, res) => {  
  const id = req.params.id;  
  // Perform data deletion with corresponding ID  
});  

 

Kammalawa

Fahimtar yadda ake sarrafa bayanan shigar da mai amfani da buƙatun HTTP daban-daban yana da mahimmanci a ci gaban yanar gizo. Ta amfani Express.js da na tsakiya kamar body-parser, zaku iya aiwatar da shigarwa cikin sauƙi daga fom da sarrafa buƙatun HTTP daban-daban gami da GET, POST, PUT, PATCH, da DELETE. Wannan yana ba ku damar ƙirƙirar fasali masu ma'amala da sassauƙa akan gidan yanar gizon ku.