CORS on custom API endpoint
This is what I have in api.ts file
import { MiddlewareConfigFn } from "wasp/server";
import { FooBar } from "wasp/server/api"; // This type is generated by Wasp based on the
export const apiMiddleware: MiddlewareConfigFn = (config) => {
return config;
};
export const fooBar: FooBar = (req, res, context) => {
res.set("Access-Control-Allow-Origin", ""); // Example of modifying headers to override Wasp default CORS middleware.
res.json({ msg:
};
// export const fooBar = (req: any, res: {
// set: (arg0: string, arg1: string) => void;
// json: (arg0: { msg: string; }) => void;
// }, context: { user: any; }) => {
// console.log("Hello from fooBar!");
// console.log("User:", context.user);
// res.set("Access-Control-Allow-Origin", ""); // Example of modifying headers to override Wasp default CORS middleware.
// res.json({ msg:
// };
And in main.wasp
api fooBar {
fn: import { fooBar } from "@src/api",
httpRoute: (GET, "/foo/bar"),
auth: true,
middlewareConfigFn: import { apiMiddleware } from "@src/api"
}
I get CORS when hitting it for some reason, as you can see I tried using it with auth,without auth with res.set("Access-Control-Allow-Origin", "*");
nothing seems to fix it, any ideas?
import { MiddlewareConfigFn } from "wasp/server";
import { FooBar } from "wasp/server/api"; // This type is generated by Wasp based on the
api declaration above.export const apiMiddleware: MiddlewareConfigFn = (config) => {
return config;
};
export const fooBar: FooBar = (req, res, context) => {
res.set("Access-Control-Allow-Origin", ""); // Example of modifying headers to override Wasp default CORS middleware.
res.json({ msg:
Hello, ${context.user ? "registered user" : "stranger"}! });};
// export const fooBar = (req: any, res: {
// set: (arg0: string, arg1: string) => void;
// json: (arg0: { msg: string; }) => void;
// }, context: { user: any; }) => {
// console.log("Hello from fooBar!");
// console.log("User:", context.user);
// res.set("Access-Control-Allow-Origin", ""); // Example of modifying headers to override Wasp default CORS middleware.
// res.json({ msg:
Hello, ${context.user ? "registered user" : "stranger"}! });// };
And in main.wasp
api fooBar {
fn: import { fooBar } from "@src/api",
httpRoute: (GET, "/foo/bar"),
auth: true,
middlewareConfigFn: import { apiMiddleware } from "@src/api"
}
I get CORS when hitting it for some reason, as you can see I tried using it with auth,without auth with res.set("Access-Control-Allow-Origin", "*");
nothing seems to fix it, any ideas?