1

I have a problem when trying to redirect routes from node.js to react router.

 app.get('*', (req,res) => {
      res.sendFile(path.resolve(__dirname + '../client/build/index.html'));
    });html 

My browser says:

Error: ENOENT: no such file or directory, stat '/home/node/app../client/build/index.html'

Here's printscreen of my directories

Btw, what __dirname does?

2
  • The path is missing a slash after app, before ../client. As for __dirname, Google it. Commented Jun 6, 2018 at 11:39
  • do you mean? /../client/build/index.html Commented Jun 6, 2018 at 11:52

1 Answer 1

1

you should use something like this...

path.resolve(__dirname, '../', 'client/build/index.html')

**Like Explained here**
path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif');
{
 if the current working directory is /home/myself/node,
 this returns '/home/myself/node/wwwroot/static_files/gif/image.gif'
}
Sign up to request clarification or add additional context in comments.

1 Comment

What if we are creating index.html on the fly using HTMLWebPack plugin? The file is basically on the server so I get same error unfortunately. How can I find the path of directory in terms of server?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.