From f06a9bb6f4174e6081c777102fe3bbdb019ee1a6 Mon Sep 17 00:00:00 2001 From: Manuel Spigolon Date: Tue, 5 Oct 2021 00:40:43 +0200 Subject: [PATCH] fix example (#155) --- README.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ddd15b4..ad83d83 100644 --- a/README.md +++ b/README.md @@ -68,15 +68,19 @@ You can use it as is without passing any option or you can configure it as expla ```js const fastify = require('fastify')() -fastify.register(require('fastify-cors'), (instance) => (req, callback) => { - let corsOptions; - // do not include CORS headers for requests from localhost - if (/localhost/.test(origin)) { - corsOptions = { origin: false } - } else { - corsOptions = { origin: true } +fastify.register(require('fastify-cors'), function (instance) { + + return (req, callback) => { + let corsOptions; + const origin = req.headers.origin + // do not include CORS headers for requests from localhost + if (/localhost/.test(origin)) { + corsOptions = { origin: false } + } else { + corsOptions = { origin: true } + } + callback(null, corsOptions) // callback expects two parameters: error and options } - callback(null, corsOptions) // callback expects two parameters: error and options }) fastify.get('/', (req, reply) => {