[SOLVED] Can’t feed React index.html file to NodeJS server

Issue

This Content is from Stack Overflow. Question asked by Answer

I’m trying to feed the index.html file from my frontend build folder and I get a strange error:
send deprecated hidden: use dotfiles: ‘allow’ instead

Here is the code I’m running:

    if (process.env.NODE_ENV === 'production') {
  app.use(
    express.static(path.join(__dirname, '../frontend/build'), {
      dotfiles: 'allow',
    })
  )

  app.get('*', (req, res) => {
    res.sendFile(__dirname, '../', 'frontend', 'build', 'index.html')
  })
} else {
  app.get('/', (req, res) => {
    res.status(200).json({ message: 'Welcome to the API' })
  })
}

Although I have set dotfiles: ‘allow’ , the error doesn’t go away. The error appears when I acces the frontend app’s main page in the browser. The main page opens, but the server crashes after that.



Solution

sendFile doesn’t allow so many arguments

You wanted to use path.join

res.sendFile(path.join(__dirname, '../', 'frontend', 'build', 'index.html'))


This Question was asked in StackOverflow by Answer and Answered by Konrad Linkowski It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?