yeps-cors
v1.0.1
Published
YEPS Cross-Origin Resource Sharing (CORS)
Downloads
23
Maintainers
Readme
YEPS CORS
YEPS Cross-Origin Resource Sharing (CORS)
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,
});