You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a custom API route for my plugin. Tried to following all the document but still got CORS. Can you guys help me explain?
src/api/custom/method/route.ts
import type { MedusaRequest, MedusaResponse } from "@medusajs/medusa";
import MBBService from "../../../../services/mbb";
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
const mbbService: MBBService = req.scope.resolve("mbbService");
const payload = req.body;
// basic validation of request body
if (!payload) {
console.log('Something is wrong with Payload');
return res.status(400).json({ error: 'Invalid payload' });
}
try {
const response = await mbbService.validateTransaction(payload);
res.json(response); // Return the actual response from validateTransaction
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: 'Failed to fetch transaction history' });
}
};
export const CORS = false
src/api/middlewares.ts
import { authenticate, type MiddlewaresConfig } from '@medusajs/medusa'
import { parseCorsOrigins } from 'medusa-core-utils'
import * as cors from 'cors'
export const config: MiddlewaresConfig = {
routes: [
{
matcher: /.*/,
middlewares: [
cors.default({ credentials: true, origin: parseCorsOrigins(process.env.STORE_CORS ?? '') }), // Update here based on needs (STORE_CORS, ADMIN_CORS...)
],
},
],
}
STORE_CORS is defined in backend.
ERROR:
Access to fetch at 'https://api.backend' from origin 'https://www.storefront' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have a custom API route for my plugin. Tried to following all the document but still got CORS. Can you guys help me explain?
src/api/custom/method/route.ts
src/api/middlewares.ts
STORE_CORS is defined in backend.
ERROR:
Access to fetch at 'https://api.backend' from origin 'https://www.storefront' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Beta Was this translation helpful? Give feedback.
All reactions