node.js TypeError: path must be absolute or specify root to res.sendFile [failed to parse JSON]

前端 未结 13 1749
长情又很酷
长情又很酷 2020-11-28 20:03

[add] So my next problem is that when i try adding a new dependence (npm install --save socket.io). The JSON file is also valid. I get this error: Failed to parse json

相关标签:
13条回答
  • 2020-11-28 20:34

    The error is pretty clear, you need to specify an absolute (instead of relative) path and/or set root in the config object for res.sendFile(). Examples:

    // assuming index.html is in the same directory as this script
    
    res.sendFile(__dirname + '/index.html');
    

    or specify a root (which is used as the base path for the first argument to res.sendFile():

    res.sendFile('index.html', { root: __dirname });
    

    Specifying the root path is more useful when you're passing a user-generated file path which could potentially contain malformed/malicious parts like .. (e.g. ../../../../../../etc/passwd). Setting the root path prevents such malicious paths from being used to access files outside of that base path.

    0 讨论(0)
提交回复
热议问题