currently I use app.use(express.static('public'))
and my files located in the public folder of my node js express app and its working good.
However, I would like to store those files (index.html, etc) in my s3 bucket (so multiple apps can use this website).
I tried
app.get('/', function(req, res) {
res.sendfile('link-to-s3-file/index.html');
});
with no success...
In your node application, you can use node-static module to serve static resources. The node-static module is an HTTP static-file server module with built-in caching. First of all, install node-static module using NPM as below. After installing node-static module, you can create static file server in Node.
html is the root and files with paths such as js/ css/ images/ taken from the root folder. Note: Its important to understand that you cannot run NodeJS in S3 and instead you will be using the internal web hosting from S3 to serve the static content.
We can do this using the AWS management console or by using Node. js. To create an S3 bucket using the management console, go to the S3 service by selecting it from the service menu: Select "Create Bucket" and enter the name of your bucket and the region that you want to host your bucket.
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'
}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With