Ele
Ele

Reputation: 33726

How can I generate the external url for images in Sails?

Currently, Sails serves the images hosted in assets/images folder, however, I need a different approach because the images will be hosted in a CDN system (AWS Cloudfront), so the images' URLs follow a structure like this:

https://hdfhhfh.cloudtfront.net/images/image.jpg

Therefore, How can I approach this?, How can I approach this in order to Sails automatically generate the URL for me?

For dev env, it's ok, for production env I need all the images in HTML/EJS to point to the CDN.

Upvotes: 5

Views: 151

Answers (2)

Ele
Ele

Reputation: 33726

After thinking about how to approach this, I decided to config a reverse proxy between the client and the backend.

With Nginx I configured a rule to redirect the /images/* to Cloudfront endpoint, this way I've avoided placing hard code in my project. Moreover, I have css files which are pointing to /images/*, with this approach I don't care about it.

Upvotes: 0

Robertino Vasilescu
Robertino Vasilescu

Reputation: 1078

What you can do after uploading the images to S3 is:

  1. In CloudFront add an alias (Alternate Domain Names) for the default https://hdfhhfh.cloudtfront.net/ like https://images.example.com/
  2. In your Sails config (e.g. custom.js) add a global var like:

    images_url: 'https://images.example.com/'

then return this URL concatenated with the images/image.jpg e.g ${images_url}images/image.jpg from your API to your frontend.

Upvotes: 3

Related Questions