如何解决使用Express从AWS S3存储桶提供整个React CRA
我基本上是试图将express用作一种反向代理。我的最终目标是为不同的用户提供不同的反应CRA包。现在,尽管我只是在进行概念验证,以查看是否有可能这样做。
TLDR目标:
- 使用express指向存储在s3存储桶中的特定CRA捆绑包并将其提供服务
这是我在快递中使用的代码:
app.get('/*',function (req,res) {
const bucketParams = {
Bucket: 'some-dumb-bucket',Key: 'cra/index.html'
};
s3.getObject(bucketParams)
.on('httpHeaders',function (statusCode,headers) {
res.set('Content-Length',headers['content-length']);
res.set('Content-Type',headers['content-type']);
res.set('Last-Modified',headers['last-modified']);
this.response.httpResponse.createUnbufferedStream()
.pipe(res);
})
.send();
})
我遇到的问题是我的所有内容都返回了错误的标题。当我进入s3并查看元数据时,它具有正确的标头,那么为什么将所有标头都提取为“ text / html”?
解决方法
我发现我做错了!它正在遍历并获取相同的index.html标头。修复:
app.get('/*',function (req,res) {
const bucketParams = {
Bucket: 'some-dumb-bucket',Key: 'auth/index.html'
};
if (req.url && req.url !== '/') {
bucketParams.Key = `auth${req.url}`;
} else
bucketParams.Key = `auth/index.html`;
// send the assets over from s3
s3.getObject(bucketParams)
.on('httpHeaders',function (statusCode,headers) {
res.set('Content-Length',headers['content-length']);
res.set('Content-Type',headers['content-type']);
res.set('Last-Modified',headers['last-modified']);
res.set('ETag',headers['etag']);
this.response.httpResponse.createUnbufferedStream()
.pipe(res);
})
.send();
});
代码可能会更干净一点,但是PoC可以工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。