Home > Back-end >  run function everytime endpoint is hit in next.js
run function everytime endpoint is hit in next.js

Time:12-16

Hey how can make an API in next.js run a function each time i hit an endpoint so i can generate random values? Right now everytime i go to /api it gives me same number.

I am reading the documentation but it does not mention this situation. When i search stackoverflow for "run function when endpoint is hit next.js" i dont get anything either, so bear with me on this probably simple question.

export default function handler(req, res) {
  let number = Math.floor(Math.random(10));
  res.status(200).json({ name: number });
}

CodePudding user response:

Math.random()

always returns between 0 and 1. use

Math.random() * 10

instead.

  • Related