1
var cors = require("cors");

cors({ origin: '*' });
cors({ allowHeaders: 'X-PINGOTHER'});
cors({ methods: 'GET,HEAD,PUT,PATCH,POST,DELETE'});

exports.endpoint = function(request, response) {
    let text = '100,000';
    response.writeHead(200, { 'Content-Type': 'text/plain' });
    response.end(text);
}

I am running this on Runkit and still get the error when checking on a website, where I want to display this return value: "No 'Access-Control-Allow-Origin' header is present on the requested resource"

blazk
  • 5
  • 2
  • Your code doesn't seem to be a complete [mcve], and your use of `cors()` doesn't seem to match the examples in [the documentation](https://www.npmjs.com/package/cors) at all. – Quentin Jan 17 '18 at 07:16
  • try to allow `OPTION` methods – Cr. Jan 17 '18 at 07:21

1 Answers1

2

In your example you've loaded the cors module and configured it, but not actually done anything to get it to intercept the HTTP request and send back your CORS headers.

If you're just using a simple Runkit endpoint, you don't need the CORS module at all – just add the headers in your endpoint, where you're already adding the Content-Type header:

exports.endpoint = function(req, res) {
    res.writeHead(200, {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': '*',
    });
    res.end('foo');
};
Charlie Schliesser
  • 7,851
  • 4
  • 46
  • 76