Runkit - No 'Access-Control-Allow-Origin' header is present on the requested resource

284 Views Asked by At
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"

1

There are 1 best solutions below

0
Charlie Schliesser On

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