Shigarwa Express .js da Ƙirƙirar App na Farko

A matsayin samfurin yaren AI, tabbas zan iya ba da cikakken jagora akan shigarwa Express.js da ƙirƙirar aikace-aikacen gidan yanar gizon ku na farko. Bari mu nutse cikin tsari-mataki-mataki:

Mataki 1: Abubuwan da ake buƙata

Kafin ka fara, tabbatar da cewa ka Node.js shigar a kan na'urarka. Kuna iya saukewa Node.js daga gidan yanar gizon hukuma( https://nodejs.org ) kuma ku bi umarnin shigarwa don tsarin aikin ku.

Mataki 2: Kafa Sabon Aiki

Bude terminal(ko command prompt) naku kuma ƙirƙirar sabon kundin adireshi don aikinku. Kewaya zuwa kundin adireshi ta amfani da cd umarni:

mkdir my-express-app  
cd my-express-app  

Mataki 3: Fara aikin

Yanzu, fara sabon Node.js aiki ta hanyar gudanar da umarni mai zuwa:

npm init

Za a umarce ku don shigar da wasu bayanai game da aikinku, kamar sunan fakiti, sigar, bayanin, wurin shigarwa, da sauransu. Kuna iya danna Enter don karɓar ƙimar da aka saba don yawancin faɗakarwa.

Mataki 4: Installing Express.js

Na gaba, kuna buƙatar shigarwa Express.js azaman abin dogaro don aikin ku. Yi amfani da umarni mai zuwa don yin haka:

npm install express --save

Wannan zai zazzagewa kuma ya girka Express.js, kuma --save tutar za ta ƙara ta azaman abin dogaro a cikin package.json fayil ɗin ku.

Mataki 5: Ƙirƙirar Express App

Yanzu lokaci ya yi da za a ƙirƙiri Express.js aikace-aikacenku na farko. Ƙirƙiri sabon fayil mai suna app.js(ko kowane sunan da kuka fi so) a cikin kundin tsarin aikin ku.

A cikin app.js, kuna buƙatar buƙatu Express da ƙirƙirar misalin sa. Ƙara lambar mai zuwa zuwa app.js fayil ɗin ku:

const express = require('express');  
const app = express();  

Mataki na 6: Saita Hanyar Hanya

Bari mu ƙirƙiri hanya mai sauƙi don amsa buƙatun HTTP masu shigowa. Misali, za mu ƙirƙiri hanyar da za ta amsa da Hello, World! duk buƙatun masu shigowa. Ƙara lambar mai zuwa zuwa app.js:

app.get('/',(req, res) => {  
  res.send('Hello, World!');  
});  

Mataki 7: Fara uwar garken

A ƙarshe, kuna buƙatar fara Express uwar garken. Ƙara lambar mai zuwa zuwa ƙarshen app.js:

const port = 3000;  
  
app.listen(port,() => {  
  console.log(`Server is running on http://localhost:${port}`);  
});  

Mataki 8: Gudanar da Aikace-aikacen

Ajiye app.js fayil ɗin ku kuma komawa zuwa terminal. Gudun umarni mai zuwa don fara Express.js uwar garken ku:

node app.js

Idan an saita komai daidai, yakamata ku ga sakon "Server yana gudana akan http://localhost:3000 " a cikin terminal.

Mataki 9: Gwada Aikace-aikacen

Bude burauzar gidan yanar gizon ku kuma kewaya zuwa http://localhost:3000. Ya kamata ku ga sakon Hello, World! da aka nuna akan shafin.

 

Taya murna! Kun yi nasarar shigar Express.js da ƙirƙirar aikace-aikacen gidan yanar gizon ku na farko. Yanzu zaku iya ginawa akan wannan tushe kuma bincika ƙarin Express.js fasalulluka da iyakoki don haɓaka ƙaƙƙarfan aikace-aikacen gidan yanar gizo masu ƙarfi. Happy codeing!