Static Files क्या हैं
express.static एक folder से files serve करता है, जैसे images, CSS, और client-side JavaScript।
In this page:
Syntax
app.use(express.static('public'));
Static Files
app.use(express.static("public")) public folder में files को URLs से map करता है, इसलिए public/logo.png /logo.png बन जाता है। आप पहले argument की तरह एक URL prefix add कर सकते हैं। इसे dynamic routes से पहले रखें ताकि files पहले मिलें।
Note:
एक reliable path के लिए path.join(__dirname, "public") इस्तेमाल करें।
उदाहरण: Static files
const express = require("express");
const path = require("path");
const app = express();
app.use(express.static(path.join(__dirname, "public")));
app.use("/assets", express.static(path.join(__dirname, "assets")));
app.listen(3000);
// public/index.html -> GET /index.html
// assets/logo.png -> GET /assets/logo.png
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- गलत working directory के relative एक path इस्तेमाल करना
- sensitive folders expose करना
- production में cache headers set न करना
चैप्टर सारांश
- express.static एक folder serve करता है
- Files URLs में map होती हैं
- ज़रूरत पड़ने पर एक prefix add करें
- Absolute paths इस्तेमाल करें
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: