30 Aug 2013

Generate a UUID using node.js

Generate RFC4122 version 1 or version 4 UUIDs in node.js.

Install the node-uuid module using npm:

$ sudo npm install node-uuid

server.js:

var http = require("http");
var uuid = require("node-uuid");

http.createServer(function(req, res) {

  // v1 (time-based)
  res.write("<p>" + uuid.v1() + "</p>");

  // v4 (random)
  res.write("<p>" + uuid.v4() + "</p>");

  res.end();

}).listen(8080);

Start:

$ node server.js

Test in your browser:

http://localhost:8080

82570700-11d3-11e3-9ac6-c5af91abd58b

5c0457bb-765f-48c3-86e1-a53a4954d8f2