Nalika nggawe aplikasi web, nangani data input pangguna minangka aspek kritis kanggo nggawe fitur interaktif lan fleksibel. Ing Express.js lingkungan pangembangan, sampeyan bisa kanthi gampang ngolah data input saka formulir lan macem-macem panjalukan HTTP kayata GET, POST, PUT, PATCH, lan DELETE. Mangkene pandhuan rinci kanthi macem-macem cara lan conto kanggo mbantu sampeyan entuk iki:
Nampa Informasi saka Form
Nggawe HTML Form: Miwiti kanthi nggawe HTML form ing file Pug utawa EJS. Priksa manawa sampeyan nyetel action
atribut ing <form>
tag kanggo nemtokake rute ing ngendi panjalukan bakal dikirim.
<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>
Nangani POST Panjaluk: Ing panangan rute, gunakake body-parser
middleware kanggo ngekstrak data saka POST panyuwunan.
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
});
Nangani Macem-macem Jinis Request karo Conto Login
Ngirim POST Panjaluk saka Login Form: Ing HTML form, priksa manawa sampeyan nyetel post
cara lan action
atribut kanggo nemtokake rute ing ngendi POST panjaluk kasebut bakal dikirim.
<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>
Nangani POST Request for Login: Ing panangan rute, gunakake body-parser
middleware kanggo ngekstrak data saka POST panyuwunan lan nindakake pangolahan login.
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!');
}
});
Penanganan PUT lan DELETE Panjaluk
Nangani PUT Panjaluk: Kanggo nangani PUT panjalukan, sampeyan bisa nggunakake rute lan middleware kanggo ngekstrak data saka panyuwunan lan nindakake nganyari sing cocog.
app.put('/update/:id',(req, res) => {
const id = req.params.id;
const updatedData = req.body;
// Perform data update with corresponding ID
});
Nangani DELETE Request: Kanggo nangani DELETE panjalukan, uga nggunakake rute lan middleware kanggo ngenali ID lan nindakake pambusakan.
app.delete('/delete/:id',(req, res) => {
const id = req.params.id;
// Perform data deletion with corresponding ID
});
Kesimpulan
Ngerteni carane nangani data input pangguna lan macem-macem panjalukan HTTP penting banget ing pangembangan web. Kanthi nggunakake Express.js lan middleware kaya body-parser
, sampeyan bisa kanthi gampang ngolah input saka formulir lan nangani panjalukan HTTP sing beda kalebu GET, POST, PUT, PATCH, lan DELETE. Iki ngidini sampeyan nggawe fitur interaktif lan fleksibel ing situs web sampeyan.