🌐 CORS middleware for modern Node.js
Go to file
v1rtl 57cf95f007
Merge pull request #3 from aarontravass/patch-2
fix actions badge
2023-12-04 09:44:35 +02:00
.github Update FUNDING.yml 2023-10-26 16:04:54 +00:00
.husky Initial commit 2021-07-20 18:29:19 +03:00
src feat: move cors to separate repo with tests and set version to v2 2021-07-30 12:35:23 +03:00
tests chore: bump dev deps 2022-03-05 15:29:23 +02:00
.eslintignore Initial commit 2021-07-20 18:29:19 +03:00
.eslintrc Initial commit 2021-07-20 18:29:19 +03:00
.gitignore Initial commit 2021-07-20 18:29:19 +03:00
.prettierignore Initial commit 2021-07-20 18:29:19 +03:00
.prettierrc Initial commit 2021-07-20 18:29:19 +03:00
LICENSE Initial commit 2021-07-20 18:29:19 +03:00
README.md Update README.md 2023-12-03 17:58:36 -05:00
commitlint.config.cjs Initial commit 2021-07-20 18:29:19 +03:00
package.json chore: bump dev deps 2022-03-05 15:29:23 +02:00
pnpm-lock.yaml chore: bump dev deps 2022-03-05 15:29:23 +02:00
rollup.config.js Initial commit 2021-07-20 18:29:19 +03:00
tsconfig.json chore: bump dev deps 2022-03-05 15:29:23 +02:00

README.md

@tinyhttp/cors

npm GitHub Workflow Status Coverage

A rewrite of expressjs/cors module.

HTTP cors header middleware.

Install

pnpm i @tinyhttp/cors

API

import { cors } from '@tinyhttp/cors'

cors(options)

Returns the CORS middleware with the settings specified in the parameters

Options

  • origin: Can be a string defining the Access-Control-Allow-Origin value, a boolean which if set to true sets the header to '*', a Regex type, an array (for multiple origins) or a function which contains the request and response as parameters and must return the value for the Access-Control-Allow-Origin header
  • methods: Array of method names which define the Access-Control-Allow-Methods header, default to all the most common methods (GET, HEAD, PUT, PATCH, POST, DELETE)
  • allowedHeaders: Configures the Access-Control-Allow-Headers CORS header. Expects an array (ex: ['Content-Type', 'Authorization']).
  • exposedHeaders: Configures the Access-Control-Expose-Headers CORS header. If not specified, no custom headers are exposed
  • credentials: Configures the Access-Control-Allow-Credentials CORS header. Set to true to pass the header, otherwise it is omitted.
  • maxAge: Configures the Access-Control-Max-Age CORS header. Set to an integer to pass the header, otherwise it is omitted.
  • optionsSuccessStatus: Provides a status code to use for successful OPTIONS requests, since some legacy browsers (IE11, various SmartTVs) choke on 204.
  • preflightContinue: Set 204 and finish response if true, call next if false.

The default configuration is:

{
  "origin": "*",
  "methods": ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"],
  "optionsSuccessStatus": 204,
  "preflightContinue": false
}

Example

import { App } from '@tinyhttp/app'
import { cors } from '@tinyhttp/cors'

const app = new App()

app
  .use(cors({ origin: 'https://myfantastic.site/' }))
  .options('*', cors())
  .get('/', (req, res) => {
    res.send('The headers contained in my response are defined in the cors middleware')
  })
  .listen(3000)