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.