Sling Academy
Home/Node.js/Fix: Node & Express Can’t Load Static Files

Fix: Node & Express Can’t Load Static Files

Last updated: December 28, 2023

Understanding the Issue

When developing a Node.js application using Express, you might encounter an issue where static files such as CSS, JavaScript, or images fail to load. This usually occurs because Express.js does not serve static files by default. You need to explicitly tell the server where to look for these files.

Steps to Fix the Issue

1. Specify Static Directory: Use express.static middleware to serve static files. For example, if your static files are in a directory named public, add the following line to your Express app:

app.use(express.static('public'));

2. Correct File Paths: Ensure the paths you use in your HTML match the file structure within your specified static directory.

Code Example

// Require express
const express = require('express');
const path = require('path');

// Initialize express app
const app = express();

// Serve static files from 'public' directory
app.use(express.static(path.join(__dirname, 'public')));

// the rest of your app logic goes here

// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`Server running on port ${PORT}`);
});

Alternative Methods

1. If you have static files in more than one directory or if you want to specify a virtual path prefix, you can use:

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

2. In this case, your file URLs will start with ‘/static’ (e.g., http://localhost:3000/static/css/style.css).

Another approach, if your static files are on a different server or Content Delivery Network (CDN), is to make sure the URLs in your HTML point to the correct remote locations.

Note

Remember to restart your Node.js server after making changes to your code.

Next Article: Node.js & Express Issue: req.body Empty – How to Fix

Previous Article: Fixing Node.js & Express Error: Request Entity Too Large

Series: Dealing with Common Errors in Node.js

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates