VOOZH about

URL: https://www.geeksforgeeks.org/node-js/how-to-serve-static-files-in-express-js/

⇱ Serving Static Files in ExpressJS - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Serving Static Files in ExpressJS

Last Updated : 25 Apr, 2026

ExpressJS provides built-in support to serve static assets efficiently, making it easy to deliver files like HTML, CSS, JavaScript, and images to clients.

  • Static middleware serves files directly based on URL paths, eliminating the need to define individual routes.
  • Simplifies frontend integration with minimal configuration.
  • Serves HTML, CSS, JavaScript, and images from the public directory.
  • Uses path.join() with __dirname to create a safe, cross-platform absolute path.

Syntax:

app.use(express.static(path.join(__dirname, 'public')));

Steps To Serve Static Files

To set up an ExpressJS application, follow these structured steps:

Step 1: Create the Project Directory

Open your terminal and create a new directory for your project

mkdir my-express-app
cd my-express-app

Step 2: Initialize the NodeJS Application

Initialize your project and create a package.json file

npm init -y

Step 3: Install ExpressJS

Install ExpressJS as a dependency

npm install express

Step 4: Set Up the Project Structure

Create the main application file and necessary directories

touch app.js
mkdir public

Project Structure

👁 Project Structure

Example : Uses express.static to serve a static CSS file to the Express server

  • Initializes an ExpressJS application.
  • Serves static files from the public directory.
  • Defines a root route (/) sending a response (overridden by index.html).
  • Starts the server on port 3000 and logs status.

Console Output:

👁 Consolelog
Console.log Output

Output:

👁 serve static files in Express JS

Comment

Explore