This project was implemented to describe how to create nextjs api general handler with cors config.
yarn add cors or npm install cors --save-dev
const routes = [
{
route: 'yourRouteName', //nextjs action name
view: (req, res)=> {
//do somthing ...
// return [your response object]
// dont use res.end
}
}
];export const generalCorsConfig = {
methods: ['POST'],
origin: ['yoursite.com'],
};in your next api file.create an index function like this:
export default async function index(req, res) {
await generalHandler(req, res, routes, generalCorsConfig);
}