admin管理员组

文章数量:1418140

I recently deployed my Node.js blog on AppFog. I plan to use a bucket on Amazon S3 to stream my assets (javascript/stylesheets/images).

How can I make sure that Express.js get static assets to my Amazon S3 bucket instead of the regular /public?

I recently deployed my Node.js blog on AppFog. I plan to use a bucket on Amazon S3 to stream my assets (javascript/stylesheets/images).

How can I make sure that Express.js get static assets to my Amazon S3 bucket instead of the regular /public?

Share Improve this question asked Feb 7, 2013 at 3:32 jpmonettejpmonette 9362 gold badges15 silver badges30 bronze badges 3
  • Why not just make the bucket public and and link directly to the assets on //s3.amazonaws./bucketname/filename.ext? – josh3736 Commented Feb 7, 2013 at 3:34
  • What do you mean? I'm migrating my old sites with url like /images/2012/... I cannot change the path. – jpmonette Commented Feb 7, 2013 at 3:36
  • I've added an answer that expands on what I mean. Why can't you change the path? – josh3736 Commented Feb 7, 2013 at 3:53
Add a ment  | 

2 Answers 2

Reset to default 8

I wouldn't stream assets through the node server – it's a waste of resources, and you're going to have to deal with HTTP caching headers.

Instead, your HTML should link directly to the S3 bucket. Instead of:

<script src="/js/script.js"></script>

Do:

<script src="//s3.amazonaws./bucket/js/script.js"></script>

Given that you're migrating, just set up a permanent redirect.

app.get(/^\/(js|css|images)\/.*/, function(req, res) {
    res.redirect(301, '//s3.amazonaws./bucket' + req.path);
});

This will redirect all requests for things in the js, css, and images folders to a S3 bucket. For example, /js/script.js would redirect to //s3.amazonaws./bucket/js/script.js.

This will help ease the transition, but you should still migrate your site's references to the S3 URLs to eliminate the unnecessary HTTP roundtrip cause by having the redirect.

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

本文标签: javascriptStreaming Assets (JSCSSimages) from Amazon S3 with ExpressjsStack Overflow