An warware: yadda ake aika fayil ษ—in html tare da express

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');
});

Shafi posts:

Leave a Comment