Skip to content

Instantly share code, notes, and snippets.

@ernestofreyreg
Last active August 10, 2021 17:59
Show Gist options
  • Save ernestofreyreg/e4b91cf5b280772dcb42038a2741c300 to your computer and use it in GitHub Desktop.
Save ernestofreyreg/e4b91cf5b280772dcb42038a2741c300 to your computer and use it in GitHub Desktop.
import { isValidEmail } from 'lib/email'
import { NextApiRequest, NextApiResponse } from 'next'
import nc from 'next-connect'
// THIS IS IN MEMORY STATE
// will reset on every app restart
const subs = []
export default nc<NextApiRequest, NextApiResponse>()
.get(async (req, res) => {
return res.json(subs)
})
.post(async (req, res) => {
if (!isValidEmail(req.body.email)) {
return res.status(400).json({ error: 'invalid-email' })
}
subs.push({ email: req.body.email, updated: Date.now() })
return res.status(204).end()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment