lundi 3 mai 2021

How to send a random value in Node.js or express.js

I had this express.js code. As shown below:

const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const randomMonth = months[Math.floor(Math.random() * months.length)];

app.get('/v1/month', (req, res) => {
    res.json({ month: randomMonth })
});

But what happens is I always get the same response which is {"month":"July"}.

How do I make it send a random response?




Aucun commentaire:

Enregistrer un commentaire