How to generate random uuid in Node and Express

#nodejs #expressjs

Last updated on Jul 10, 2022 by Suraj Sharma



In this tutorial you will learn how you can generate random unique IDs(uuid) in node.js


First we will need to install the npm uuid module


npm install --save uuid

To generate random uuid we will use uuid.v4() method


Example


const express = require('express')
const uuid = require('uuid');
const app = express();

const port = 5000;

app.get('/api/random-uuid', async (req, res) => {
  const id = uuid.v4();
  res.send({id});
}

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
});


Related Solutions


Rate this post


Suraj Sharma is a Full Stack Software Engineer. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.