How do I create a URL shortener?

后端 未结 30 2057
我寻月下人不归
我寻月下人不归 2020-11-22 05:11

I want to create a URL shortener service where you can write a long URL into an input field and the service shortens the URL to \"http://www.example.org/abcdef\

30条回答
  •  别跟我提以往
    2020-11-22 05:41

    Here is a Node.js implementation that is likely to bit.ly. generate a highly random seven-character string.

    It uses Node.js crypto to generate a highly random 25 charset rather than randomly selecting seven characters.

    var crypto = require("crypto");
    exports.shortURL = new function () {
        this.getShortURL = function () {
            var sURL = '',
                _rand = crypto.randomBytes(25).toString('hex'),
                _base = _rand.length;
            for (var i = 0; i < 7; i++)
                sURL += _rand.charAt(Math.floor(Math.random() * _rand.length));
            return sURL;
        };
    }
    

提交回复
热议问题