cors-anywhere
cors-anywhere copied to clipboard
Not able to self host, Not working as intended.
// Listen on a specific host via the HOST environment variable
var host = process.env.HOST || '0.0.0.0';
// Listen on a specific port via the PORT environment variable
var port = process.env.PORT || 8080;
var cors_proxy = require('cors-anywhere');
cors_proxy.createServer({
originWhitelist: [], // Allow all origins
requireHeader: ['origin', 'x-requested-with'],
removeHeaders: ['cookie', 'cookie2']
}).listen(port, host, function() {
console.log('Running CORS Anywhere on ' + host + ':' + port);
});
base code from example and export PORT=8080 and export CORSANYWHERE_WHITELIST=https://my.domain.com/
Endpoints return
This API enables cross-origin requests to anywhere.
Usage:
/ Shows help
/iscorsneeded This is the only resource on this host which is served without CORS headers.
/<url> Create a request to <url>, and includes CORS headers in the response.
If the protocol is omitted, it defaults to http (https if port 443 is specified).
Cookies are disabled and stripped from requests.
Redirects are automatically followed. For debugging purposes, each followed redirect results
in the addition of a X-CORS-Redirect-n header, where n starts at 1. These headers are not
accessible by the XMLHttpRequest API.
After 5 redirects, redirects are not followed any more. The redirect response is sent back
to the browser, which can choose to follow the redirect (handled automatically by the browser).
The requested URL is available in the X-Request-URL response header.
The final URL, after following all redirects, is available in the X-Final-URL response header.
To prevent the use of the proxy for casual browsing, the API requires either the Origin
or the X-Requested-With header to be set. To avoid unnecessary preflight (OPTIONS) requests,
it's recommended to not manually set these headers in your code.
Demo : https://robwu.nl/cors-anywhere.html
Source code : https://github.com/Rob--W/cors-anywhere/
Documentation : https://github.com/Rob--W/cors-anywhere/#documentation
You've shown the server part only. Where is the client-side code?
And note that the environment variable only works if you use the default server.js from CORS Anywhere. The code snippet at the top uses cors-anywhere as a library, with an empty originWhitelist. If you want to restrict requests to a specific origin, use originWhitelist: ["https://my.domain.com"], instead (note: / is not at the end because an origin is port + :// + host (hostname + optional port separator & port).
async function fetchInfo() {
const res = await fetch(`${CORS}${URL}`).then(async data => {
let d = await data.json();
...
})
setTimeout(fetchInfo, 15000)
}
fetchInfo()
Something like this on the client side.
Let me try cloning repo.
If I use the http://IP:8080 of the instance it seems to work, I have configured vhosts for the port 8080 and works on https://my.domain.com and it is behind a cloudflare.
Also If I use the server.js after cloning it again works with http://IP:8080 and not https://my.domain.com
So CORS Anywhere and your application are at the same origin?
If so, you need to set the X-Requested-With request header, so that CORS Anywhere allows the request to go through.
CORS Anywhere requires either the Origin header or X-Requested-With to prevent direct requests (when visiting in the browser), because that's would be a huge security issue for your website. See my comments at https://github.com/Rob--W/cors-anywhere/issues/39 for more info.
No, they do not have same origin. Application is at (https://application.domain.com) and wants to use the cors server https://cors.domain.com/
The request structure from the client side will be https://cors.domain.com/https://api-resource.com/endpoint and client is expecting a JSON response. BTW https://cors-anywhere.herokuapp.com/ seems to work fine.
Could it be that your server (in front of Node.js) normalizes URLs, such that /http://example.com/ becomes /http:/example.com/? That is a common mistake, see e.g. https://github.com/Rob--W/cors-anywhere/issues/143 (nginx) and https://github.com/Rob--W/cors-anywhere/issues/201 (Apache).
No, doesn't look like it the client points to the right URL.
this is the config
<VirtualHost *:80>
ServerName cors.domain.com
ServerAlias cors.domain.com
ServerAdmin webmaster@localhost
ProxyPreserveHost Off
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I don't understand what you're trying to say in your last comment.
In any case, have you tried putting a different Node.js application behind it, to see if the request.url is as expected?
require("http").createServer((req, res) => {
res.end(req.url);
}).listen(8080);
and then send the request and check whether the returned URL matches the input URL.