How can I serve static web site from s3 through node expressjs? How can I serve static web site from s3 through node expressjs? express express

How can I serve static web site from s3 through node expressjs?


I wouldn't reinvent the wheel. There's a reasonably recent and well documented middleware module for this on npm

From the docs:

app.get('/media/*', s3Proxy({  bucket: 'bucket_name',  prefix: 'optional_s3_path_prefix',  accessKeyId: 'aws_access_key_id',  secretAccessKey: 'aws_secret_access_key',  overrideCacheControl: 'max-age=100000'}));


I wanted /foo to match a /foo.html on a different s3 bucket. I used the following to do so:

app.get('/:thePath', function(req, res) {    var key = "saved/" + req.params.thePath;    if(key.indexOf(".html") === -1) {        key = key + ".html";    }    s3.getObject({ Bucket: "just-read", Key: key })   .on('httpHeaders', function (statusCode, headers) {        res.set('Content-Length', headers['content-length']);        res.set('Content-Type', "text/html");        this.response.httpResponse.createUnbufferedStream()            .pipe(res);    })    .send();});