W
Wasp3w ago
fustuk.

Weird CORS Error

This is my cors middleware that works on my other endpoints* Also the code for the endpoint I'm trying to invoke, whtever I do im hitting CORS, everything else works properly...
No description
No description
No description
No description
2 Replies
kapa.ai
kapa.ai3w ago
Hi @fustuk.. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the information provided in the knowledge sources, it appears that you're experiencing a CORS (Cross-Origin Resource Sharing) issue with a specific endpoint in your Wasp application. This is a common problem that several Wasp users have encountered. Let me provide some insights and potential solutions: 1. CORS Configuration: Wasp includes CORS middleware by default, but sometimes it may need additional configuration, especially for custom API endpoints. As mentioned in the Wasp documentation on Custom HTTP API Endpoints, APIs are designed to be flexible and don't utilize the default middleware like Operations do. 2. Using apiNamespace: One solution that has worked for other users is to use the apiNamespace feature in Wasp. This allows you to apply middleware configuration to all APIs under a specific path. Here's an example:
apiNamespace myApi {
middlewareConfigFn: import { myApiNamespaceMiddlewareFn } from "@src/apis",
path: "/api"
}
apiNamespace myApi {
middlewareConfigFn: import { myApiNamespaceMiddlewareFn } from "@src/apis",
path: "/api"
}
3. CORS for OPTIONS requests: A common pitfall is forgetting to set CORS for OPTIONS requests. The apiNamespace approach can help with this as it applies to all methods under the specified path. 4. Middleware Order: In some cases, the order of middleware registration can affect CORS behavior. As mentioned in a GitHub issue comment, one user solved their CORS issue by specifying app.use(cors()) before registering the router after ejecting from Wasp. 5. Checking Server-Side Logs: It's recommended to add console.log() statements in your server-side code (both middleware and action) to see how far it gets before throwing an error. This can help identify if the issue is actually a CORS error or if it's being masked as one. If you're still experiencing issues after trying these approaches, it might be helpful to create a minimal reproducible example and share it with the Wasp team. They are aware of some confusion around CORS configuration and are working on improving this aspect of the framework, as evidenced by this open GitHub issue. Remember, when working with custom API endpoints in Wasp, you may need to explicitly enable CORS for those endpoints to work properly from the client side. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @fustuk., I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
fustuk.
fustuk.OP3w ago
damn I forgot about namespaces, issue resolved

Did you find this page helpful?