Last active
July 31, 2024 10:11
-
-
Save infomiho/5ca98e5e2161df4ea78f76fc858d3ca2 to your computer and use it in GitHub Desktop.
Multiple domains for CORS - custom global middleware in Wasp 0.12+
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app corsTest { | |
wasp: { | |
version: "^0.14.0" | |
}, | |
title: "cors-test", | |
server: { | |
middlewareConfigFn: import { getGlobalMiddleware } from "@src/data", | |
} | |
} | |
route RootRoute { path: "/", to: MainPage } | |
page MainPage { | |
component: import { MainPage } from "@src/MainPage" | |
} | |
query getSomeData { | |
fn: import { getSomeData } from "@src/data", | |
entities: [] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cors from "cors"; | |
import { MiddlewareConfigFn } from "wasp/server"; | |
import { GetSomeData } from "wasp/server/operations"; | |
export const getSomeData: GetSomeData = async (_args, _context) => { | |
return { | |
someData: "Hello from the server!", | |
}; | |
}; | |
export const getGlobalMiddleware: MiddlewareConfigFn = (config) => { | |
const isDevelopment = process.env.NODE_ENV === "development"; | |
const clientUrl = process.env.WASP_WEB_CLIENT_URL ?? "http://localhost:3000"; | |
// Allow all origins in development, otherwise only allow the client URL. | |
const origin = isDevelopment ? "*" : [clientUrl, 'http://someotherurl.com']; | |
// Remove the default setup and provide a new custom setup for the CORS middleware | |
config.delete("cors"); | |
config.set( | |
"cors", | |
cors({ | |
origin, | |
}) | |
); | |
return config; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment