Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example in read me does not work at all #215

Closed
2 tasks done
jacob-orbiit opened this issue Jul 29, 2022 · 6 comments · Fixed by #216
Closed
2 tasks done

Example in read me does not work at all #215

jacob-orbiit opened this issue Jul 29, 2022 · 6 comments · Fixed by #216

Comments

@jacob-orbiit
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.3.0

Plugin version

8.0.0

Node.js version

18.7.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

12.4

Description

The plugin does literally nothing. The response is not decorated and the plugin seems to have not even run.

Steps to Reproduce

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

const fastify = Fastify();

await fastify.register(middie);
await fastify.register(cors);

I also tried the example from fastify.io, and that crashes immediately:

await fastify.register(middie);
fastify.use(cors());
[…]/@fastify/cors/index.js:20
  fastify.decorateRequest('corsPreflightEnabled', false)
          ^
TypeError: Cannot read properties of undefined (reading 'decorateRequest')

Expected Behavior

No response

@mcollina
Copy link
Member

mcollina commented Jul 29, 2022

Good post! We have not updated the README.

This should work:

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

const app = fastify()

await app.register(cors, { 
  // put your options here
})

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

await app.listen(3000)

I think you are confusing two things, this module and https://www.npmjs.com/package/cors.
You need middie only to use the cors middleware; otherwise, you can just use this plugin.

mcollina added a commit that referenced this issue Jul 29, 2022
@jacob-orbiit
Copy link
Author

jacob-orbiit commented Jul 29, 2022

Thanks for the response!

Sorry, when you say fastify.register(cors, {}), to which fastify are you referring—the library or the instance? (naming them the same in the docs is extremely confusing).

@mcollina
Copy link
Member

I'm sorry but I don't see where the confusion is with require('cors') vs require('@fastify/cors). They are clearly two different modules.

@jacob-orbiit
Copy link
Author

I'm not talking about cors vs fastify/cors (although that was also quite confusing). I'm talking about fastify.register(). What is fastify there? Is it the default factory export of this library or the instance created by that factory?

@mcollina
Copy link
Member

Good spot! I've updated the example.

@jacob-orbiit
Copy link
Author

Thanks! That ambiguity exists thorough the entire fastify docs btw.

The new/updated example you posted is exactly what I tried, and it didn't work (unless middie is interfering?).

@Eomm Eomm closed this as completed in b4d7977 Jul 29, 2022
@Eomm Eomm closed this as completed in #216 Jul 29, 2022
@Uzlopak Uzlopak reopened this Jul 29, 2022
@Uzlopak Uzlopak closed this as completed Jul 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants