nextjs cors
v2.2.0
Nextjs-Cors是一個Node.js軟件包,可提供一個中間件,可用於在NextJS應用程序中啟用具有各種選項的CORS。
首先,我們將不得不安裝,以便使用此精美的軟件包。
# Using npm
npm install nextjs-cors@latest
# Using yarn
yarn add nextjs-cors@latest
# Using pnpm
pnpm add nextjs-cors@latestNextJS-Cors使用CORS軟件包,因此我們邀請您檢查文檔https://github.com/expressjs/cors
頁/api/whoami。 {ts,js}
import NextCors from 'nextjs-cors' ;
async function handler ( req , res ) {
// Run the cors middleware
// nextjs-cors uses the cors package, so we invite you to check the documentation https://github.com/expressjs/cors
await NextCors ( req , res , {
// Options
methods : [ 'GET' , 'HEAD' , 'PUT' , 'PATCH' , 'POST' , 'DELETE' ] ,
origin : '*' ,
optionsSuccessStatus : 200 , // some legacy browsers (IE11, various SmartTVs) choke on 204
} ) ;
// Rest of the API logic
res . json ( { message : 'Hello NextJs Cors!' } ) ;
} nextjs-cors受許可。