Case: I want to keep *.js files for users of my website on the Hapi (Node.js) server, so they can include them CDN-like (i.e: jQuery, font-awesome).
Question: What is the proper way to do so on Hapi (Node.js) server?
What I already tried was just to link them:
<script src="https://mywebsite.com/myscripts/myjsfile.js"></script>
Or to declare route, using Inert plugin basing on Future Studio Tutorial and Hapi Documentation:
server.route({
method: "GET",
path: '/myscripts/{path}',
handler: {
directory: {
path: '/myscripts/',
}
}
}
And then link them in html script tags (like above). What I've got was:
{"statusCode":403,"error":"Forbidden"}
I test it on Heroku and all the plugins I use are:
Solution was pretty straightforward:
auth to false,handler()'s directory()'s listing to trueRoute, which allows every website to access static files from www.example.com/myscripts/whatever.js
server.route({
method: "GET",
path: '/myscripts/{path}',
config: {
auth: false,
cors: { origin: ['*'] },
handler: {
directory: {
path: 'myscripts',
listing: true
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With