Home > Blockchain >  How to use fastify-cors and node JS to allow multiple urls in cors origin
How to use fastify-cors and node JS to allow multiple urls in cors origin

Time:12-28

My URL list look like this

const urls = ["example1.com", "exam.com"]

My cors setting look like this:

  const cors = require("fastify-cors")

  // Enable CORS
  fastify.register(cors, {
    origin: '*',
    methods: ['GET', 'POST'],
    credentials: true
  })

CodePudding user response:

From the Fasify-Cors docs you can use array in origin option. Read for more

Your example could be:

const cors = require("@fastify/cors")

  // Enable CORS
  fastify.register(cors, {
    origin: ["example1.com", "exam.com"],
    methods: ['GET', 'POST'],
    credentials: true
  })

CodePudding user response:

fastify-cors has been deprecated. Please use @fastify/cors instead.

For your version, this is the documentation.

const cors = require("fastify-cors")
const urls = ["example1.com", "exam.com"]

// Enable CORS
fastify.register(cors, {
  origin: urls,
  methods: ['GET', 'POST'],
  credentials: true
})
  • Related