Rumah > Soal Jawab > teks badan
P粉4389183232023-08-30 11:03:32
Jika anda mahu nextjs
中使用 cors
库,我为其创建了一个库 nextjs-cors
.
https://www.npmjs.com/nextjs-cors
https://github.com/yonycalsin/nextjs-cors
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!' }); }
P粉0445262172023-08-30 09:16:21
Saya jumpa penyelesaiannya di sini:
Pada asasnya, saya hanya perlu menambah fail next.config.js dalam direktori root dan tambah yang berikut:
// next.config.js module.exports = { async rewrites() { return [ { source: '/api/:path*', destination: 'https://api.example.com/:path*', }, ] }, };