Skip to content

This project was implemented to describe how to create nextjs api general handler with cors config.

Notifications You must be signed in to change notification settings

l0ger/nextjs-cors-general-handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nextjs-cors-general-handler

This project was implemented to describe how to create nextjs api general handler with cors config.

Usage:

1-install cors middleware using below command:

yarn add cors or npm install cors --save-dev

2- go your next api file and define your route array like this:
const routes = [
  {
    route: 'yourRouteName', //nextjs action name
    view: (req, res)=> { 
     //do somthing ...
     // return [your response object]
     // dont use res.end
    }
    }
];
3- define your cors config object, for example:
export const generalCorsConfig = {
  methods: ['POST'],
  origin: ['yoursite.com'],
};
4- import generalHandler from ./utils/nextGeneralHandler

in your next api file.create an index function like this:

export default async function index(req, res) {
  await generalHandler(req, res, routes, generalCorsConfig);
}

About

This project was implemented to describe how to create nextjs api general handler with cors config.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published