错误非常明显,您需要指定一个绝对路径(而不是相对路径)和/或
root在的config对象中设置
res.sendFile()。例子:
// assuming index.html is in the same directory as this scriptres.sendFile(__dirname + '/index.html');
或指定一个根(用作第一个参数的基本路径
res.sendFile():
res.sendFile('index.html', { root: __dirname });root当您传递用户生成的文件路径时,指定路径可能会更有用,该文件路径可能包含格式不正确/恶意的部分
..(例如
../../../../../../etc/passwd)。设置
root路径可防止此类恶意路径用于访问该基本路径之外的文件。


![node.js TypeError:路径必须是绝对路径,或为res.sendFile指定根目录[无法解析JSON] node.js TypeError:路径必须是绝对路径,或为res.sendFile指定根目录[无法解析JSON]](http://www.mshxw.com/aiimages/31/391097.png)
