personal-site/pages/api/test-route.ts

46 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-08-21 08:14:02 +00:00
import { NextApiHandler, NextApiRequest, NextApiResponse } from "next";
2023-08-21 08:32:27 +00:00
import Cors from "cors";
const cors = Cors({
2023-08-21 08:34:40 +00:00
methods: ["GET"],
origin: "*",
2023-08-21 08:32:27 +00:00
});
// Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
function runMiddleware(req: NextApiRequest, res: NextApiResponse, fn: Function) {
return new Promise((resolve, reject) => {
fn(req, res, (result: any) => {
if (result instanceof Error) {
return reject(result);
}
return resolve(result);
});
});
}
2023-08-21 08:14:02 +00:00
/**
* @type {NextApiHandler}
*/
2023-08-21 08:32:27 +00:00
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
2023-08-21 08:39:44 +00:00
// res.setHeader("Access-Control-Allow-Credentials", "true");
2023-08-21 08:32:27 +00:00
// res.setHeader("Access-Control-Allow-Origin", "*");
// res.setHeader("Access-Control-Allow-Methods", "*");
// res.setHeader("Access-Control-Allow-Headers", "*");
2023-08-21 08:39:44 +00:00
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");
2023-08-21 08:18:53 +00:00
2023-08-21 08:39:44 +00:00
// await runMiddleware(req, res, cors);
res.json({
2023-08-21 08:14:02 +00:00
title: "Hello There",
message: "General Kenobi",
});
}