Express res.sendfile throwing forbidden error

前端 未结 3 770
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 22:08

I have this code:

res.sendfile( \'../../temp/index.html\' )

However, it throws this error:

Error: Forbidden
at SendStream.e         


        
3条回答
  •  野性不改
    2020-11-29 22:31

    This answer gathers together the info from the other answers/comments.

    It depends whether you want to include something relative to the process working directory (cwd) or the file directory. Both use the path.resolve function (put var path = require('path') at the top of the file.

    • relative to cwd: path.resolve('../../some/path/to/file.txt');
    • relative to file: path.resolve(__dirname+'../../some/path/to/file.txt');

    From reading the link from @Joe's comment, it sounds like relative paths are a security risk if you accept user input for the path (e.g. sendfile('../.ssh/id_rsa') might be a hacker's first try).

提交回复
热议问题