Babban matsalar da ke da alaฦa da aika fayilolin HTML tare da Express ita ce Express ba ta goyan bayan hidimar fayiloli na asali kamar HTML, CSS, da JavaScript. Don yin amfani da fayiloli na tsaye, dole ne ku yi amfani da matsakaiciyar matsakaici kamar express.static() ko express.static middleware wanda kunshin sabis-tsaye ya samar. Wannan middleware zai ba ku damar saka kundin adireshi inda fayilolinku na tsaye suke sannan sannan taswirar buฦatun waษancan fayilolin zuwa waccan adireshin.
To send an HTML file with Express, you can use the res.sendFile() method. This method takes the path of the file as its argument and sends it to the client. Example: app.get('/', (req, res) => { res.sendFile(__dirname + '/index.html'); });
1. app.get ('/', (req, res) => {
// Wannan layin yana bayyana mai sarrafa hanya don tushen hanyar aikace-aikacen. Lokacin da aka yi buฦatu zuwa tushen hanyar, wannan aikin dawo da kira za a aiwatar da shi tare da req kuma ya sa abubuwa azaman hujjarsa.
2. res.sendFile (__dirname + '/index.html');
// Wannan layin yana amfani da hanyar Express sendFile() don aika fayil ษin HTML da ke __dirname + '/index.html' ga abokin ciniki a matsayin amsa ga buฦatarsu ta tushen hanyar aikace-aikacen.
Menene fayil ษin HTML
Fayil ษin HTML fayil ne na Hapertext Markup Language, wanda ake amfani dashi don ฦirฦirar shafukan yanar gizo. Fayilolin HTML sun ฦunshi tags da halaye waษanda ke ayyana tsari da abun ciki na shafin yanar gizon. An rubuta su da rubutu a sarari, don haka ana iya buษe su da gyara su tare da kowane editan rubutu.
Bayani na ExpressJS
ExpressJS 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. An kira shi ainihin tsarin sabar uwar garken na Node.js.
ExpressJS yana ba da ฦaฦฦarfan tsarin fasali don haษaka aikace-aikacen yanar gizo da wayar hannu. Yana sauฦaฦa aiwatar da buฦatun buฦatun, sarrafa kayan masarufi, samar da shafukan HTML da aika martani ga ษangaren abokin ciniki. ExpressJS kuma yana ba da tallafi don injunan samfuri kamar Jade, EJS da Handlebars.
Tsarin ExpressJS yana dogara ne akan JavaScript kuma yana amfani da tsarin gine-gine na MVC (Model-View-Controller) wanda ke taimaka wa masu haษakawa ฦirฦirar aikace-aikace masu ฦima cikin sauฦi. Bugu da ฦari, yana ba masu haษaka damar amfani da bayanai masu yawa kamar MongoDB, Redis, MySQL da dai sauransu, wanda ke sauฦaฦe gina aikace-aikace masu rikitarwa.
Ta yaya zan aika fayil ษin HTML ta amfani da Express
Don aika fayil ษin HTML ta amfani da Express, kuna buฦatar amfani da hanyar res.sendFile(). Wannan hanyar tana ษaukar hanyar fayil ษin azaman hujja kuma tana aika shi azaman amsa ga abokin ciniki.
Example:
app.get ('/', (req, res) => {
res.sendFile (__dirname + '/index.html');
});