@sugo/cors
v1.0.10
Published
Middleware for setting the CORS headers.
Downloads
11
Readme
@sugo/cors
Middleware for setting the CORS headers.
How to install
npm install --save @sugo/cors
getCorsMiddleware
Builds a middleware function that sets the CORS headers. Can receive an object to define the value of the cors headers
Default options
export const defaultOptions: ICorsOptions = {
'access-control-allow-credentials': true,
'access-control-allow-headers': '',
'access-control-allow-methods': 'GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS',
'access-control-allow-origin': '*',
'access-control-expose-headers': 'authorization',
'access-control-max-age': '2592000',
};
Default middleware
This package includes a Cors middleware built with the default options for development purposes.
Example - Node Http Server
import { defaultOptions, getCorsMiddleware } from '@sugo/cors';
const server = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => {
res.setHeader('Content-Type', 'application/json');
const corsMiddleware = getCorsMiddleware();
corsMiddleware(req, res);
res.end('{}');
});
Example - SuGo Server
import { defaultOptions, getCorsMiddleware } from '@sugo/cors';
const corsMiddleware = getCorsMiddleware();
const server = createServer((req: SuGoRequest, res: SuGoResponse) => res.json({})).useMiddleware(corsMiddleware);
Example - default middleware
import cors from '@sugo/cors';
const server = createServer((req: SuGoRequest, res: SuGoResponse) => res.json({})).useMiddleware(cors);