Home

Awesome

@fastify/cors

CI NPM version neostandard javascript style

@fastify/cors enables the use of CORS in a Fastify application.

Install

npm i @fastify/cors

Compatibility

Plugin versionFastify version
^10.x^5.x
^8.x^4.x
^7.x^3.x
^3.x^2.x
^1.x^1.x

Please note that if a Fastify version is out of support, then so are the corresponding version(s) of this plugin in the table above. See Fastify's LTS policy for more details.

Usage

Require @fastify/cors and register it as any other plugin, it will add an onRequest hook and a wildcard options route.

import Fastify from 'fastify'
import cors from '@fastify/cors'

const fastify = Fastify()
await fastify.register(cors, {
  // put your options here
})

fastify.get('/', (req, reply) => {
  reply.send({ hello: 'world' })
})

await fastify.listen({ port: 3000 })

You can use it as is without passing any option or you can configure it as explained below.

Options

:warning: DoS attacks

The use of RegExp or a function for the origin parameter might allow an attacker to perform a Denial of Service attack. Craft those with extreme care.

Configuring CORS Asynchronously

const fastify = require('fastify')()

fastify.register(require('@fastify/cors'), (instance) => {
  return (req, callback) => {
    const corsOptions = {
      // This is NOT recommended for production as it enables reflection exploits
      origin: true
    };

    // do not include CORS headers for requests from localhost
    if (/^localhost$/m.test(req.headers.origin)) {
      corsOptions.origin = false
    }

    // callback expects two parameters: error and options
    callback(null, corsOptions)
  }
})

fastify.register(async function (fastify) {
  fastify.get('/', (req, reply) => {
    reply.send({ hello: 'world' })
  })
})

fastify.listen({ port: 3000 })

Custom Fastify hook name

By default, @fastify/cors adds a onRequest hook where the validation and header injection are executed. This can be customized by passing hook in the options. Valid values are onRequest, preParsing, preValidation, preHandler, preSerialization, and onSend.

import Fastify from 'fastify'
import cors from '@fastify/cors'

const fastify = Fastify()
await fastify.register(cors, {
  hook: 'preHandler',
})

fastify.get('/', (req, reply) => {
  reply.send({ hello: 'world' })
})

await fastify.listen({ port: 3000 })

When configuring CORS asynchronously, an object with delegator key is expected:

const fastify = require('fastify')()

fastify.register(require('@fastify/cors'), {
  hook: 'preHandler',
  delegator: (req, callback) => {
    const corsOptions = {
      // This is NOT recommended for production as it enables reflection exploits
      origin: true
    };

    // do not include CORS headers for requests from localhost
    if (/^localhost$/m.test(req.headers.origin)) {
      corsOptions.origin = false
    }

    // callback expects two parameters: error and options
    callback(null, corsOptions)
  },
})

fastify.register(async function (fastify) {
  fastify.get('/', (req, reply) => {
    reply.send({ hello: 'world' })
  })
})

fastify.listen({ port: 3000 })

Acknowledgements

The code is a port for Fastify of expressjs/cors.

License

Licensed under MIT.<br/> expressjs/cors license