setupCors
is a utility function that sets up Cross-Origin Resource Sharing (CORS) middleware for a Next.js Route Handlers. It allows you to define the CORS configuration options and applies them to incoming requests, adding the appropriate CORS headers to the response.
To use setupCors
, you'll need to install the route-handlers-cors
package as a dependency:
# Using npm
npm install --save route-handlers-cors@latest
# Using yarn
yarn add route-handlers-cors@latest
# Using pnpm
pnpm add route-handlers-cors@latest
Here's an example of how to use setupCors
in a Next.js Route Handlers:
import setupCors from 'route-handlers-cors';
import { NextResponse, NextRequest } from "next/server";
export async function GET(request: NextRequest) => {
// Set up CORS for this API route
await setupCors(request, NextResponse.next, {
// Define your CORS configuration options here
origin: 'https://example.com', // origin or *
methods: ['GET', 'POST'],
allowedHeaders: ['Authorization'],
});
// Handle your API route logic here
// ...
// Return your API response
return NextResponse.json({ success: true });
};
route-handlers-cors
is an open source project licensed by MIT. If you would like to support the project or become a sponsor, please contact me here.
- Github @mrruby
- Twitter @getbold_eth
Thanks to the wonderful people who collaborate with me !
route-handlers-cors
under License.