Streaming Assets (JS / CSS / Images) from Amazon S3 with Express.js
2 answers
I would not pass resources through a node server - it is a waste of resources and you have to deal with HTTP cache headers.
Instead, your HTML should link directly to the S3 bucket. Instead:
<script src="/js/script.js"></script>
do:
<script src="//s3.amazonaws.com/bucket/js/script.js"></script>
Considering that you are migrating, just set up a permanent redirect.
app.get(/^\/(js|css|images)\/.*/, function(req, res) {
res.redirect(301, '//s3.amazonaws.com/bucket' + req.path);
});
This will redirect all requests for things in the js, css and images folders to the S3 bucket. For example, / js / script.js will be redirected to // s 3.amazonaws.com/bucket/js/script.js.
This will help ease the transition, but you should move your site links to S3 URLs to eliminate unnecessary HTTP call reason by redirecting it.
+8
source to share
If you really need to do this, use pipe () from the requests library: https://github.com/mikeal/request#streaming
0
source to share