← Back to Node.js Course | Chapter 7: Express.js Basics | Lesson 5 of 7

Static Files क्या हैं

express.static एक folder से files serve करता है, जैसे images, CSS, और client-side JavaScript।

In this page:

  1. Static Files
Syntax
javascript
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

bash
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. #}
आम गलतियां
  1. गलत working directory के relative एक path इस्तेमाल करना
  2. sensitive folders expose करना
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.