Home > other >  Express Send Files in Folder
Express Send Files in Folder

Time:10-12

I have a server with files like this:

- index.js
/client
 -index.html
 -about.html
 -signup.html
 -style.css

In index.js I have a express server.

  • When the user goes to / , it should go to index.html
  • When the user goes to /about.html it should go to about.html etc
  • When the user goes to /style.css it should go to style.css etc..

I know you can do it like this..

app.get('/', (req, res) => {
  res.sendFile(__dirname   '/client/index.html')
})

But I want to make it so that it automatically sends the correct file without having to get each folder.

I know it has something to do with app.use but I am not fully sure how to use it.

CodePudding user response:

Whoops, I never thought it would be this easy-

app.use('/', express.static('client'))

  • Related