---
title: "How can you serve static files using Express?"  
description: "How can you serve static files using Express?"  
author: "Ravi Vishwakarma"  
published: 2025-04-04  
updated: 2025-04-04  
canonical: https://www.mindstick.com/interview/34026/how-can-you-serve-static-files-using-express  
category: "javascript"  
tags: ["javascript", "express js"]  
reading_time: 2 minutes  

---

# How can you serve static files using Express?

You can serve static files (like HTML, CSS, JS, images, etc.) in **Express** using the built-in middleware `express.static`.

## Basic Setup:

```javascript
const express = require('express');
const app = express();

// Serve static files from the "public" folder
app.use(express.static('public'));

app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});
```

## Example Folder Structure:

```plaintext
project/
├── public/
│   ├── index.html
│   ├── style.css
│   └── script.js
└── app.js
```

With this setup:

`http://localhost:3000/index.html` serves the `index.html` file.

`http://localhost:3000/style.css` serves the CSS file, etc.

## You can also mount it at a specific route:

```javascript
app.use('/static', express.static('public'));
```

Now your files will be accessible like:

- `http://localhost:3000/static/index.html`

**Also Read:** [How do you define a route in Express?](https://www.mindstick.com/interview/34025/how-do-you-define-a-route-in-express)

## Answers

### Answer by Ravi Vishwakarma

You can serve static files (like HTML, CSS, JS, images, etc.) in **Express** using the built-in middleware `express.static`.

## Basic Setup:

```javascript
const express = require('express');
const app = express();

// Serve static files from the "public" folder
app.use(express.static('public'));

app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});
```

## Example Folder Structure:

```plaintext
project/
├── public/
│   ├── index.html
│   ├── style.css
│   └── script.js
└── app.js
```

With this setup:

`http://localhost:3000/index.html` serves the `index.html` file.

`http://localhost:3000/style.css` serves the CSS file, etc.

## You can also mount it at a specific route:

```javascript
app.use('/static', express.static('public'));
```

Now your files will be accessible like:

- `http://localhost:3000/static/index.html`

**Also Read:** [How do you define a route in Express?](https://www.mindstick.com/interview/34025/how-do-you-define-a-route-in-express)


---

Original Source: https://www.mindstick.com/interview/34026/how-can-you-serve-static-files-using-express

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
