首页 > 解决方案 > 错误:ENOENT:没有这样的文件或目录 index.html Node.js

问题描述

尝试从 node.js 重定向路由以响应路由器时出现问题。

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

我的浏览器说:

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

这是我的目录的打印屏幕

顺便说一句,__dirname 是做什么的?

标签: node.jsreactjsexpress

解决方案


你应该使用这样的东西......

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'
}

推荐阅读