UNPKG

rjweb-server

Version:

Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS

26 lines (25 loc) 1.46 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Middleware_1 = __importDefault(require("../classes/Middleware")); exports.default = new Middleware_1.default('Built-in CORS Middleware', '1.0.0') .httpRequest((config, server, context, ctr, end) => { if (config.allowAll || config.origins?.length) ctr.headers.set('access-control-allow-origin', config.allowAll ? '*' : config.origins?.join(', ')); if (config.allowAll || config.methods?.length) ctr.headers.set('access-control-allow-methods', config.allowAll ? '*' : config.methods?.join(', ')); if (config.allowAll || config.headers?.length) ctr.headers.set('access-control-allow-headers', config.allowAll ? '*' : config.headers?.join(', ')); if (config.allowAll || config.exposeHeaders?.length) ctr.headers.set('access-control-expose-headers', config.allowAll ? '*' : config.exposeHeaders?.join(', ')); if (config.allowAll || config.credentials) ctr.headers.set('access-control-allow-credentials', config.allowAll ? '*' : 'true'); if (config.maxAge) ctr.headers.set('access-control-max-age', config.maxAge.toString()); if (ctr.url.method === 'OPTIONS' && !context.route) { return end(ctr.status(ctr.$status.NO_CONTENT)); } }) .export();