Express res.sendfile throwing forbidden error

前端 未结 3 766
被撕碎了的回忆
被撕碎了的回忆 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

    I believe it's because of the relative path; the "../" is considered malicious. Resolve the local path first, then call res.sendfile. You can resolve the path with path.resolve beforehand.

    var path = require('path');
    res.sendFile(path.resolve('temp/index.html'));
    

提交回复
热议问题