如何直接访问我在 Hapi (Node.js) 服务器上的文件?
How do I give direct access to my files on Hapi (Node.js) server?
案例:我想在 Hapi (Node.js) 服务器上为我网站的用户保留 *.js 文件,以便他们可以像 CDN 一样包含它们(即:jQuery,字体很棒)。
问题:在 Hapi (Node.js) 服务器上这样做的正确方法是什么?
我已经尝试过 link 他们:
<script src="https://mywebsite.com/myscripts/myjsfile.js"></script>
或者声明路由,使用Inert plugin basing on Future Studio Tutorial and Hapi Documentation:
server.route({
method: "GET",
path: '/myscripts/{path}',
handler: {
directory: {
path: '/myscripts/',
}
}
}
然后 link 它们在 html 脚本标签中(如上)。我得到的是:
{"statusCode":403,"error":"Forbidden"}
我在 Heroku 上测试它,我使用的所有插件是:
- 哈皮
- 路径
- 乔伊
- 惰性
- 车把
- MongoDB
- 愿景
- AuthCookie
- 铃铛
- 加密
解决方案非常简单:
- 允许跨源请求,
- 将
auth
设为false
,
- 将
handler()
的directory()
的listing
设置为true
路由,允许每个网站访问静态文件
www.example.com/myscripts/whatever.js
server.route({
method: "GET",
path: '/myscripts/{path}',
config: {
auth: false,
cors: { origin: ['*'] },
handler: {
directory: {
path: 'myscripts',
listing: true
}
}
}
}
案例:我想在 Hapi (Node.js) 服务器上为我网站的用户保留 *.js 文件,以便他们可以像 CDN 一样包含它们(即:jQuery,字体很棒)。
问题:在 Hapi (Node.js) 服务器上这样做的正确方法是什么?
我已经尝试过 link 他们:
<script src="https://mywebsite.com/myscripts/myjsfile.js"></script>
或者声明路由,使用Inert plugin basing on Future Studio Tutorial and Hapi Documentation:
server.route({
method: "GET",
path: '/myscripts/{path}',
handler: {
directory: {
path: '/myscripts/',
}
}
}
然后 link 它们在 html 脚本标签中(如上)。我得到的是:
{"statusCode":403,"error":"Forbidden"}
我在 Heroku 上测试它,我使用的所有插件是:
- 哈皮
- 路径
- 乔伊
- 惰性
- 车把
- MongoDB
- 愿景
- AuthCookie
- 铃铛
- 加密
解决方案非常简单:
- 允许跨源请求,
- 将
auth
设为false
, - 将
handler()
的directory()
的listing
设置为true
路由,允许每个网站访问静态文件 www.example.com/myscripts/whatever.js
server.route({
method: "GET",
path: '/myscripts/{path}',
config: {
auth: false,
cors: { origin: ['*'] },
handler: {
directory: {
path: 'myscripts',
listing: true
}
}
}
}