Home > Net >  How to Serving static files with express if it is inside a subdirectory
How to Serving static files with express if it is inside a subdirectory

Time:12-31

I am having trouble with serving a html file which is inside a subdirectory inside public folder. My directory structure looks like:

public/
      HTML/
           index.html
index.js

I want to serve the index.html for the root route.

This doesn't work:

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

CodePudding user response:

According to the docs, this should be enough:

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

It also gives you such example

http://localhost:3000/images/kitten.jpg
http://localhost:3000/css/style.css
http://localhost:3000/js/app.js
http://localhost:3000/images/bg.png
http://localhost:3000/hello.html

It doesn't go much further other than explaining how you can set up multiple public directories. Anyway, it has worked for me in my projects.

Perhaps you need to state directly app.use(express.static('html')) or app.use(express.static('public/html')). Let me know what works for you.

  • Related