Categories
JavaScript Answers

How to fix Express.js not getting static files?

Spread the love

Sometimes, we want to fix Express.js not getting static files.

In this article, we’ll look at how to fix Express.js not getting static files.

How to fix Express.js not getting static files?

To fix Express.js not getting static files, we call express.staric with the path to the static files.

For instance, we write

app.use("/styles", express.static(__dirname + '/styles'));

to call app.use with the path to the static files and the middleware returned by expres.static called with the path to the /styles folder in the directory.

__dirname is the path of the Express project folder.

As a result, we should be able to access the files in the styles folder with the /styles path.

Conclusion

To fix Express.js not getting static files, we call express.staric with the path to the static files.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *