Tabbas, bari mu fara!
Express.js ko kuma kawai Express tsarin tsarin aikace-aikacen gidan yanar gizo ne don Node.js, wanda aka fito dashi azaman software mai kyauta kuma mai buษewa ฦarฦashin lasisin MIT. An tsara shi don gina aikace-aikacen yanar gizo da APIs. Yana da daidaitaccen tsarin uwar garken don Node.js.
Express ba ya ษoye fasalin fasalin Node.js, amma yana sauฦaฦa shi kuma yana inganta ingantaccen sa. Yana ba da ฦaฦฦarfan tsarin fasali don aikace-aikacen yanar gizo da wayar hannu. Tare da ษimbin hanyoyin amfani da HTTP da na tsakiya a hannunku, ฦirฦirar API mai ฦarfi yana da sauri da sauฦi.
Bari mu nutse cikin **maganin *** kuma mu tattauna Express daki-daki.
Shigar da Express
Don shigar da Express, za mu yi amfani da Node Package Manager (npm). Gudun umarni mai zuwa a cikin tashar ku don shigar da Express a cikin aikace-aikacenku:
npm install express
Saita Sabar
Yanzu, bari mu saita sabar mai sauฦi a cikin ฦa'idar da ake kira "app.js".
const express = require('express') const app = express() const port = 3000 app.get('/', (req, res) => res.send('Hello World!')) app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
A cikin wannan ainihin misalin, mun ฦirฦiri sabar gidan yanar gizo mai sauฦi wanda ke amsawa da "Hello Duniya!" don buฦatun zuwa shafin gida.
Express yana ba da sauฦin ฦirฦira da gudanar da sabar yanar gizo tare da Node.js. Ka lura da yadda muke aika martanin "Hello Duniya!" zuwa browser.
Rarraba a cikin Express
bayar da kwatance shine yadda ฦarshen aikace-aikacen ke amsa buฦatun abokin ciniki. Express yana sarrafa hanyoyin da nagarta sosai.
app.get('/', function (req, res) { res.send('Home Page Route') }) app.get('/about', function (req, res) { res.send('About Page Route') }) app.get('/portfolio', function (req, res) { res.send('Portfolio Page Route') }) app.get('/contact', function (req, res) { res.send('Contact Page Route') })
Ka'idar tana amsawa tare da kirtani don takamaiman hanya, kamar yadda aka nuna a sama.
A ฦarshe, yana da kyau a lura cewa Express ya zama tsarin da ake amfani da shi sosai saboda sauฦi da aikin sa. Hakanan ya haษa da fasalulluka don kewayawa, hidimar fayil a tsaye, tsaka-tsaki, injunan samfuri, da ฦari. Kayan aiki iri-iri ne don gina APIs kuma yana da babban jeri na tsawo don buฦatu daban-daban. Happy codeing!