An warware: bayyana

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!

Shafi posts:

Leave a Comment