I would probably do a database schema, where you have an id field which is set to be unique and then the url. When you save a new URL, you randomize the id and save. If it fails (id exists), just randomize and save again..
With mongoose for example you can define key to be unique by just setting { unique: true }