A cikin Express.js, routing mahimmin ra'ayi ne wanda ke ba ku damar ayyana yadda aikace-aikacenku ke tafiyar da buƙatun HTTP masu shigowa daga masu amfani. Hanyoyi suna ba ku damar tantance takamaiman ayyuka lokacin da masu amfani suka aika buƙatun zuwa takamaiman URLs akan aikace-aikacenku.
Mataki 1: Ƙirƙirar Tushen Route
Don ƙirƙirar route in Express.js, kuna amfani da app.METHOD(PATH, HANDLER)
hanyar abin aikace-aikacen( app
) don yin rajista route don takamaiman hanyar HTTP da hanyar HANYA. HANDLER aikin mai sarrafa ne wanda za'a kira shi lokacin da bukata ta same shi route.
Misali, don ƙirƙirar route mai sarrafa GET
buƙatun zuwa /hello
, zaku iya amfani da lambar mai zuwa:
app.get('/hello',(req, res) => {
res.send('Hello, this is the /hello route!');
});
Mataki 2: Karɓar Buƙatun da Amsoshi
A cikin aikin mai kulawa, zaku iya sarrafa buƙatun masu shigowa daga masu amfani kuma ku amsa ta amfani da req
(buƙatun) da res
(amsa) abubuwa. Abun req
ya ƙunshi bayanai game da buƙatun mai shigowa, kamar sigogin URL, bayanan da aka aiko, adireshin IP na mai aikawa, da sauransu. Abun res
ya ƙunshi hanyoyin amsa buƙatar, kamar res.send()
, res.json()
, res.render()
, da sauransu.
Mataki na 3: Gudanar da Hanyoyi da yawa
Express.js yana ba ku damar ayyana hanyoyi da yawa don URL ɗaya tare da hanyoyin HTTP daban-daban. Misali:
app.get('/hello',(req, res) => {
res.send('Hello, this is the GET /hello route!');
});
app.post('/hello',(req, res) => {
res.send('Hello, this is the POST /hello route!');
});
Mataki na 4: Sarrafar da Ma'auni mai ƙarfi
Hakanan zaka iya ayyana hanyoyin da ke ƙunshe da sigogi masu ƙarfi, da ma'auni( :
). Misali:
app.get('/users/:id',(req, res) => {
const userId = req.params.id;
res.send(`Hello, this is the GET /users/${userId} route!`);
});
Lokacin da mai amfani ya yi buƙatu zuwa /users/123
, userId
madaidaicin zai sami ƙimar "123".
Mataki 5: Rarrabe Routing tare da Module
A cikin manyan ayyuka, ƙila kuna son raba hanyoyi zuwa fayiloli daban-daban don kiyaye lambar tushen ku ta tsara da sarrafa su. Kuna iya amfani da su module.exports
don ayyana hanyoyi a cikin fayiloli daban sannan kuma shigo da su cikin babban fayil ɗin. Misali:
// routes/users.js
const express = require('express');
const router = express.Router();
router.get('/profile',(req, res) => {
res.send('This is the /profile route in users.js!');
});
module.exports = router;
// app.js
const usersRouter = require('./routes/users');
app.use('/users', usersRouter);
Mataki 6: Gudanar da hanyoyin da ba su wanzu
A ƙarshe, idan mai amfani ya buƙaci abin da ba ya wanzu route, zaku iya ayyana 404 route don sarrafa shi. Ana yin wannan ta hanyar saita tsoho route a ƙarshen babban fayil ɗin ku:
app.use((req, res, next) => {
res.status(404).send('Route not found!');
});
Mun koyi yadda ake ƙirƙira da sarrafa hanyoyin cikin Express.js. Ta amfani da wannan fasalin, zaku iya keɓancewa da gudanar da buƙatun mai amfani a sassauƙa da ƙarfi, mai sa aikace-aikacenku ya zama mai daidaitawa da daidaitawa. Ci gaba da bincike da amfani da hanyoyi a cikin gina ƙaƙƙarfan aikace-aikacen gidan yanar gizo masu ban sha'awa!