Skip to content

How can I use one API route from within another one? #11210

Answered by timneutkens
aej11a asked this question in Help
Discussion options

You must be logged in to vote

The way you've outlined with fetching /api/auth/emailAvailable actually slows down the endpoint, instead you can import the function that you were going to execute.

Eg:

// pages/api/auth/emailAvailable.js

export function emailAvailable(email) {
  // Just an example
  return db.find(email).length > 0
}

export default (req, res) => {
  return res.json({ available: emailAvailable(req.body.email) })
}
// pages/api/auth/register.js
import {emailAvailable} from './emailAvailable'

export default (req, res) => {
  const available = emailAvailable(req.body.email)
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@aej11a
Comment options

Answer selected by timneutkens
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants