When a request comes in for a page, eg app.get("/")
I want to return a static HTML page from amazon s3. I know I can request it from S3 and then send it, but that seems slow. Is there anyway to tell the requester to get the file from s3 directly without changing the url?
Thanks.
Failing that, what's the fastest way to serve the file from s3?
This tutorial shows writing the file first
http://www.hacksparrow.com/node-js-amazon-s3-how-to-get-started.html
// We need the fs module so that we can write the stream to a file
var fs = require('fs');
// Set the file name for WriteStream
var file = fs.createWriteStream('slash-s3.jpg');
knox.getFile('slash.jpg', function(err, res) {
res.on('data', function(data) { file.write(data); });
res.on('end', function(chunk) { file.end(); });
});
Is there a way to send the file without writing it first? Writing it seems awfully slow.
As you suspected, you cannot get the requester to fetch from S3 directly without changing the URL. You have to proxy the remote page:
var http = require('http'),
express = require('express'),
app = express();
app.get('/', function(req, res) {
http.get('http://www.stackoverflow.com', function(proxyRes) {
proxyRes.pipe(res);
});
});
app.listen(8080);
You can cache the remote page for better performance.
If the s3 page is truly static (e.g. changes rarely) your server could certainly load it the first time it was requested and cache it; that wouldn't cause any appreciable slowdown.
Otherwise, I'd rethink the "without changing the URL" requirement; the most common "solutions" are hacks generally involving frames.