2

I'm looking for a professional technique to generate unique URL addresses. I created a node.js server that should give out these URLs to accessing clients. How can I provide the URLs?

// user is connecting to www.privatebox.de

// server serves index.html with unique ID

// e.g. www.privatebox.de/8yfuzyzzm7
user1477955
  • 1,652
  • 8
  • 23
  • 35

1 Answers1

11

For a robust solution, I would consider using node-uuid to generate UUIDs.

Install the package with NPM:

npm install node-uuid

Based on the sample code from the GitHub project page:

var uuid = require('node-uuid');

// Generate a v1 (time-based) id
var timeBasedID = uuid.v1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a'

// Generate a v4 (random) id
var randomID = uuid.v4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1'

var url = 'www.privatebox.de/' + randomID;  // or + timeBasedID

If you're looking for a shorter, more url-friendly unique ID, then ShortId might be a decent option for you, although the chance of collisions will be higher. ShortId will generate ids like this:

ShortId.generate() -> 'PPBqWA9'

Finally, I suggest you look at this SO question for generating unique ids in javascript.

Community
  • 1
  • 1
Subfuzion
  • 1,789
  • 20
  • 18
  • your ShortId Link directed mit to the same page like the SO question Link is that right? – user1477955 Mar 28 '13 at 22:51
  • Sorry about that -- fixed link. – Subfuzion Mar 28 '13 at 22:53
  • 2
    The one problem of ShortId module is '-' and '_' chars in short url. A lot of sites convert them with urlEncoding and you can loose functionality. I recommend to use https://www.npmjs.com/package/id-shorter (id-shorter) that free from this limitation. – Andrey Hohutkin Dec 06 '15 at 09:49