Streaming Assets (JS / CSS / Images) from Amazon S3 with Express.js

I recently posted my Node.js blog on AppFog. I am planning on using a bucket on Amazon S3 to stream my assets (javascript / stylesheets / images).

How can I make sure Express.js gets static assets in my Amazon S3 bucket instead of the regular one /public

?

+3


source to share


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


If you really need to do this, use pipe () from the requests library: https://github.com/mikeal/request#streaming



0


source







All Articles