Skip to content

route-handlers-cors is a utility function library that sets up Cross-Origin Resource Sharing (CORS) middleware for a Next.js Route Handlers

License

Notifications You must be signed in to change notification settings

mrruby/route-handlers-cors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

route-handlers-cors is a CORS middleware library for Next.js Route Handlers

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.

Installation

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

Usage

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 });
};

Support for

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.

Stay in touch

Contributors

Thanks to the wonderful people who collaborate with me !

License

route-handlers-cors under License.

About

route-handlers-cors is a utility function library that sets up Cross-Origin Resource Sharing (CORS) middleware for a Next.js Route Handlers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published