cors-anywhere deployment with Docker Compose
My ad-tracker
Express app serves up queued advertisements with a client-side call to Javascript’s fetch
function. This, of course, raises the issue of Cross-Origin Resource Sharing.
I use the cors-anywhere
node
module to allow sites with ad-tracker
advertisements to access the server. Naturally, docker-compose
is my preferred deployment tool.
Set up the project
Create a project directory and initialize the application with npm
:
|
|
Follow the npm init
prompts.
Once initialized, add the cors-anywhere
module to the project:
|
|
Copy and paste the following into index.js
(or whatever entry-point you specified in the initialization step):
|
|
This code is take verbatim from the cors-anywhere
documentation.
To execute the application:
|
|
If it executes successfully, you should see:
|
|
Exit the app.
Docker
To create the Dockerized application image, paste the following into Dockerfile
:
|
|
This will build the cors-anywhere
into a Docker node
container.
Docker Compose
Paste the following into docker-compose.yml
:
|
|
Build the image and deploy in one step:
|
|
The last line of the console output should read:
|
|
At this point, any request proxied through the cors-anywhere-server
will be allowed access to cross-domain resources. Your client-side fetch
calls can now leverage this functionality by prefixing the destination URL with the cors-anywhere-server
URL. It may look something like this:
(function() {
var CORS_SERVER = 'https://cors-server.example.com:8080';
var AD_SERVER = 'https://ads.example.com';
fetch(CORS_SERVER + '/' + AD_SERVER).then(function(response) {
return response.json();
}).then(function(json) {
console.log('CORS request successful!');
console.log(json);
});
})();
Done!