Skip to content

Instantly share code, notes, and snippets.

@tpjnorton
Created November 9, 2021 12:25
Show Gist options
  • Save tpjnorton/ac814805485f8bb2a2652425eefd0f1c to your computer and use it in GitHub Desktop.
Save tpjnorton/ac814805485f8bb2a2652425eefd0f1c to your computer and use it in GitHub Desktop.
A more complicated Next API route handler
export default function handler(req, res) {
if (req.method === 'POST') {
checkAuth();
doSomething();
res.status(200).json({ name: 'John Doe' })
} else if (req.method === 'DELETE') {
checkAuth();
doSomethingElse();
res.status(200).json({ status: 'Deleted' })
} else if (req.method === 'GET') { // no auth
const items = doAThirdThing();
res.status(200).json({ data: items })
} else {
res.status(405).text('Method Not Allowed')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment