From 1f3ddaab52aa6a41a78fe3efb55fbac01b706e19 Mon Sep 17 00:00:00 2001 From: Tben Date: Mon, 21 Aug 2023 09:39:44 +0100 Subject: [PATCH] updates --- next.config.js | 13 +++++++++++++ pages/api/test-route.ts | 12 ++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/next.config.js b/next.config.js index 76ff8eb..fbdfda8 100644 --- a/next.config.js +++ b/next.config.js @@ -13,4 +13,17 @@ module.exports = { }, ], }, + async headers() { + return [ + { + source: "/api/test-route", + headers: [ + { key: "Access-Control-Allow-Credentials", value: "true" }, + { key: "Access-Control-Allow-Origin", value: "*" }, + { key: "Access-Control-Allow-Methods", value: "GET,OPTIONS,PATCH,DELETE,POST,PUT" }, + { key: "Access-Control-Allow-Headers", value: "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version" }, + ], + }, + ]; + }, }; diff --git a/pages/api/test-route.ts b/pages/api/test-route.ts index 501b47e..bcc5e89 100644 --- a/pages/api/test-route.ts +++ b/pages/api/test-route.ts @@ -24,13 +24,21 @@ function runMiddleware(req: NextApiRequest, res: NextApiResponse, fn: Function) * @type {NextApiHandler} */ export default async function handler(req: NextApiRequest, res: NextApiResponse) { + // res.setHeader("Access-Control-Allow-Credentials", "true"); // res.setHeader("Access-Control-Allow-Origin", "*"); // res.setHeader("Access-Control-Allow-Methods", "*"); // res.setHeader("Access-Control-Allow-Headers", "*"); - await runMiddleware(req, res, cors); + res.setHeader("Access-Control-Allow-Credentials", "true"); + res.setHeader("Access-Control-Allow-Origin", "*"); + // another common pattern + // res.setHeader('Access-Control-Allow-Origin', req.headers.origin); + res.setHeader("Access-Control-Allow-Methods", "GET,OPTIONS,PATCH,DELETE,POST,PUT"); + res.setHeader("Access-Control-Allow-Headers", "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version"); - res.status(200).json({ + // await runMiddleware(req, res, cors); + + res.json({ title: "Hello There", message: "General Kenobi", });