【问题标题】:How can I access secure files with express?如何使用 express 访问安全文件?
【发布时间】:2020-11-04 13:10:56
【问题描述】:
nodeapp
   -public
            -CSS
                       -style.css
   -pictures
            -secretImage.png
   -views
            -index.hbs
            -login.hbs
            -profile.hbs
   -server.js

const staticFiles = path.join(__dirname, './public')
app.use(express.static(staticFiles))
app.set('view engine', 'hbs')

我将我的 css 保存在以 html 访问的公共目录中,如下所示: href="/css/style.css" 这很好,但我需要存储一些应该只对登录用户可用的图片。如果我的图片在图片文件夹中,我该如何访问它们?

【问题讨论】:

    标签: express handlebars.js private secure-coding


    【解决方案1】:

    您可以使用sendFile 方法...

    app.get('/picture/:pictureName', (req, res) => {
         const valid = /* Do your logic to grant access */
    
         if (valid === false) {
             return res.status(403).send('Not allowed')
         }
    
         res.sendFile('your file path')
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多