【问题标题】:Error: ENOENT: no such file or directory, stat 'E:\client\index.html'错误:ENOENT:没有这样的文件或目录,stat 'E:\client\index.html'
【发布时间】:2019-03-08 10:00:57
【问题描述】:

我正在尝试从 angular 上的服务发送 http 方法到 nodejs 上的 api 到它向服务器请求但出现此错误:

Error: ENOENT: no such file or directory, stat 'E:\client\index.html'

这是我的代码:

//rutas
app.use('/', express.static('client', {redirect:false}));
app.use('/api', insumos_routes);

app.get('*', function(req, res, next){
     res.sendFile(path.resolve('/client/index.html'));   
});

//export
module.exports= app;

已经尝试过:

res.sendFile('/client/index.html');   
res.sendFile('./index.html');   

...就像它说别人的答案

【问题讨论】:

  • 能否提供项目的完整目录结构?

标签: node.js api http


【解决方案1】:

path.resolve 需要两个或更多参数,如果它有用的话。你需要给它一个基本路径。

例如:

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

您希望该路径相对于脚本所在的目录。

【讨论】:

  • 该文件相对于您的脚本在哪里?您可能需要通过使用'../client/index.html' 等进行导航来调整它。
  • 我在 client/src/index.html 中有这个文件,但已经输入了它并且不起作用
  • 您需要说明该文件与上述脚本的关系。如果它在client/src/index.html 中,那么client/index.html 找到它的希望为零,因为src/ 是必要的路径组件。
  • ...是的,我用 src 正确设置了路由,但是这个错误只通过 delete 方法传递,当我使用浏览器时,它不会通过邮递员客户端传递
  • 是的,我在 src 中输入了正确的路径但不起作用,只有当我将此路由放在浏览器上时才会通过,当我使用邮递员时它会起作用
猜你喜欢
  • 2021-09-22
  • 2021-04-04
  • 1970-01-01
  • 2020-06-03
  • 2019-05-08
  • 1970-01-01
  • 2020-12-05
  • 2021-04-26
  • 2021-08-08
相关资源
最近更新 更多