How to use cors-anywhere with my express app?

1.3k views Asked by At

I am trying to make cors-anywhere to work in my nodejs/express without success.

First I use express-cors-anywhere like this :

const anywhere = require('express-cors-anywhere')
app.use("/cors-anywhere", anywhere())

but get the following error :

anywhere is not a function

I've tried to use the native cors-anywhere library but I don't know how to implement this on an express js app :

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);
});

Any suggestion ?

1

There are 1 answers

0
user2004165 On

How to integrate with existing expressjs server #81

let proxy = corsAnywhere.createServer({
  originWhitelist: [], // Allow all origins
  requireHeaders: [], // Do not require any headers.
  removeHeaders: [] // Do not remove any headers.
});

/* Attach our cors proxy to the existing API on the /proxy endpoint. */
api.get('/proxy/:proxyUrl*', (req, res) => {
  req.url = req.url.replace('/proxy/', '/'); // Strip '/proxy' from the front of the URL, else the proxy won't work.
  proxy.emit('request', req, res);
});