Skip to content
This repository has been archived by the owner on Jan 22, 2024. It is now read-only.

caviajs/http-cors

Repository files navigation

@caviajs/http-cors

a micro framework for node.js

Introduction

Cross-Origin Resource Sharing is a mechanism that allows resources to be requested from another domain.

Usage

Installation

npm install @caviajs/http-cors --save

Configure the interceptor

Config builder

import { HttpCors } from '@caviajs/http-cors';
import { Interceptor } from '@caviajs/http-router';

export const CorsInterceptor: Interceptor = HttpCors.setup(request => {
  return {
    /*
      'Access-Control-Allow-Credentials'?: boolean;
      'Access-Control-Allow-Headers'?: string[];
      'Access-Control-Allow-Methods'?: ('DELETE' | 'GET' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'POST' | 'PUT')[];
      'Access-Control-Allow-Origin'?: string;
      'Access-Control-Expose-Headers'?: string[];
      'Access-Control-Max-Age'?: number;
    */
  };
});

Config object

import { HttpCors } from '@caviajs/http-cors';
import { Interceptor } from '@caviajs/http-router';

export const CorsInterceptor: Interceptor = HttpCors.setup({
  /*
    'Access-Control-Allow-Credentials'?: boolean;
    'Access-Control-Allow-Headers'?: string[];
    'Access-Control-Allow-Methods'?: ('DELETE' | 'GET' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'POST' | 'PUT')[];
    'Access-Control-Allow-Origin'?: string;
    'Access-Control-Expose-Headers'?: string[];
    'Access-Control-Max-Age'?: number;
  */
});

Bind the interceptor

httpRouter
  .intercept(CorsInterceptor);