This document might be outdated relative to the documentation in English. For the latest updates, please refer to the documentation in English.
Serverda statik fayllar bu rasmlar, CSS, JavaScript va fayllarni misol qilish mumkin, ular Expressda o’rnatilgan Expressda o’rnatilgan express.static
middleware orqali ko’rsatiladi.
Statik fayllarni qayerda joylashini ko’rsatish uchun express.static
oraliq qayta ishlovchisiga direktoriya nomini jo’nating.
Masalan, siz o’z rasmlaringizni, CSS, JavaScriptlarni public
direktoriyasida saqlamoqchi bo’lsangiz unda quyidagicha bo’ladi
app.use(express.static('public'));
Undan keyin public
direktoriyasini ko’rsatmagan holda statik fayllarni yuklashingiz mumkin bo’ladi:
http://localhost:3000/images/kitten.jpg
http://localhost:3000/css/style.css
http://localhost:3000/js/app.js
http://localhost:3000/images/bg.png
http://localhost:3000/hello.html
Agar siz ko’pgina direktoriyani statik qilmoqchi bo’lsangiz unda, express.static
oraliq qayta ishlovchisini yana foydalanishingiz mumkin:
app.use(express.static('public'));
app.use(express.static('files'));
Fayllar ketma-ketlik bo’yicha statik direktoriyadan joy olishda va express.static
orqali o’rnatiladi.
Agar siz “virtual” (huddi manzil lekin fayl sistemada mavjud emas) fayllardan oldin prefix qo’shimchalik yaratmoqchi bo’lsangiz, express.static
ikkita argument jo’nating, qo’shimcha ma’lumotni esa bu yerdan olishingiz mumkin bo’ladi. Ishlatishga misol esa:
app.use('/static', express.static('public'));
Endi esa public
direktoriyansidagi statik fayllarni “/static” prefiksi orqali olinadi.
http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html
Agarda siz express.static
orqali ko’rsatgan direktoriyangiz boshqa joyda ishga tushmasa, Siz uning absolyut manzilini ko’rsatishingiz kerak bo’ladi, masalan u mana bunday bo’ladi:
app.use('/static', express.static(__dirname + '/public'));