这是我的文件夹结构:
- 根文件夹
- ...
- >...
- ...
- app.js
- escape_iframe.html
- ...
从 app.js
我正在做的事情:
res.sendFile('escape_iframe.html' , {root : __dirname});
我收到的错误是:
Error: ENOENT, stat '/app/escape_iframe.html'
我认为这是一种可怕的方式来表示在提供的路径中找不到该文件?
无论如何,正如你所知,我只是想提供一个 app.js
的 同级
文件(这是 res 的文件形式) .sendFile(...
正在调用)
我还尝试过:res.sendFile('escape_iframe.html');
并且我得到:路径必须是绝对路径或指定res.sendFile的根目录
我做错了什么?
最佳答案
使用 .
指定的路径是相对于当前工作目录的,而不是相对于脚本文件的。因此,如果您运行 node app.js 可能会找到该文件,但如果您运行 nodefolder/app.js 则不会找到该文件。
要创建相对于脚本的路径,必须使用 __dirname
变量。
res.sendFile(__dirname + '/path/to/escape_iframe.html');
关于Node.js - res.sendFile - 错误 : ENOENT, stat - 即使路径正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32288193/