Skip to content

jsbase/cors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CORS Example

Below is the code from pages/_middleware.ts:

import type { NextRequest } from 'next/server'
import cors from '../lib/cors'

export async function middleware(req: NextRequest) {
  // `cors` also takes care of handling OPTIONS requests
  return cors(
    req,
    new Response(JSON.stringify({ message: 'Hello World!' }), {
      status: 200,
      headers: { 'Content-Type': 'application/json' },
    })
  )
}

Test it out with:

curl -i -X OPTIONS -H 'origin: https://vercel.com' https://jsbase-cors.vercel.app

Demo

https://jsbase-cors.vercel.app

How to Use

You can choose from one of the following two methods to use this repository:

One-Click Deploy

Deploy the example using Vercel:

Deploy with Vercel

Clone and Deploy

Download this repository via git:

git clone https://github.com/vercel/examples.git

Execute create-next-app with npm or Yarn to bootstrap the example:

npx create-next-app --example https://github.com/vercel/examples/tree/main/edge-functions/cors cors
# or
yarn create next-app --example https://github.com/vercel/examples/tree/main/edge-functions/cors cors

Next, run Next.js in development mode:

npm install
npm run dev

# or

yarn
yarn dev

Deploy it to the cloud with Vercel (Documentation).

Releases

No releases published

Packages

No packages published