Cross-Origin Resource Sharing (CORS)
JavaScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
tests
.editorconfig
.eslintrc
.gitignore
.istanbul.yml
.travis.yml
LICENSE
README.md
appveyor.yml
index.js
package.json

README.md

YEPS CORS

YEPS Cross-Origin Resource Sharing (CORS)

NPM

npm version Build Status Coverage Status Linux Build Windows Build

Dependency Status devDependency Status NSP Status

License GitHub stars GitHub forks GitHub issues Twitter

How to install

npm i -S yeps-cors

How to use

const App = require('yeps');
const Router = require('yeps-router');

const error = require('yeps-error');
const logger = require('yeps-logger');
const server = require('yeps-server');

const cors = require('yeps-cors');

const app = new App();
const router = new Router();

app.all([
    error(),
    logger(),
    cors(),
]);

router.get('/url').then(async (ctx) => {
    ctx.res.statusCode = 200;
    ctx.res.end(data); 
});

app.then(router.resolve());

server.createHttpServer(app);

Config

{String|Function(ctx)} origin Access-Control-Allow-Origin, default is request Origin header

cors({ origin: '*' });

cors({
    origin(ctx) {
        return ctx.req.url !== '/forbin' ? '*' : false;
    },
});

{String|Array} allowMethods Access-Control-Allow-Methods, default is 'GET,HEAD,PUT,POST,DELETE,PATCH'

cors({
    allowMethods: 'GET',
});

cors({
    allowMethods: ['GET', 'POST'],
});
            
cors({
    allowMethods: null,
});

{String|Array} exposeHeaders Access-Control-Expose-Headers

cors({
    exposeHeaders: 'content-length',
});

cors({
    exposeHeaders: ['content-length', 'x-header'],
});

{String|Array} allowHeaders Access-Control-Allow-Headers

cors({
    allowHeaders: 'X-PINGOTHER',
});
            
cors({
    allowHeaders: ['X-PINGOTHER'],
});

{String|Number} maxAge Access-Control-Max-Age in seconds

cors({
    maxAge: 3600,
});
                
cors({
    maxAge: '3600',
});

{Boolean} credentials Access-Control-Allow-Credentials

cors({
    credentials: true,
});

YEPS documentation