【问题标题】:How do I give direct access to my files on Hapi (Node.js) server?如何直接访问 Hapi (Node.js) 服务器上的文件?
【发布时间】:2018-12-28 16:43:13
【问题描述】:

案例:我想在 Hapi (Node.js) 服务器上为我网站的用户保留 *.js 文件,以便他们可以像 CDN 一样包含它们(即:jQuery、font-很棒)。
问题:在 Hapi (Node.js) 服务器上这样做的正确方法是什么?

我已经尝试过的只是链接它们:

<script src="https://mywebsite.com/myscripts/myjsfile.js"></script>

或者声明路由,使用Inert plugin基于Future Studio TutorialHapi Documentation

server.route({
  method: "GET",
  path: '/myscripts/{path}',
  handler: {
    directory: {
      path: '/myscripts/',
    }
  }
}

然后将它们链接到 html 脚本标签中(如上)。我得到的是:

{"statusCode":403,"error":"Forbidden"}

我在Heroku 上测试过,我使用的所有插件都是:

  • 哈皮
  • 路径
  • 乔伊
  • 惰性
  • 车把
  • MongoDB
  • 愿景
  • AuthCookie
  • 钟声
  • Bcrypt

【问题讨论】:

    标签: javascript node.js hapijs


    【解决方案1】:

    解决方案非常简单:

    1. 允许跨源请求,
    2. auth设置为false
    3. handler()directory()listing设置为true

    Route,它允许每个网站访问静态文件 www.example.com/myscripts/whatever.js

    server.route({
      method: "GET",
      path: '/myscripts/{path}',
      config: {
        auth: false,
        cors: { origin: ['*'] },
        handler: {
          directory: {
            path: 'myscripts',
            listing: true
          }
        }
      }
    }
    

    【讨论】:

    • 在您的代码中,您错误地拼写了listring,应该是listing
    猜你喜欢
    • 1970-01-01
    • 2016-03-22
    • 2011-04-10
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 2012-05-01
    相关资源
    最近更新 更多