Introduction:: Sometimes we often see a very short website in the microblog or some marketing messages. When you open it in the browser and check it, you find that you are redirected to a very long website. This is actually the service using the short website. Today, I will simply make a summary, combined with my previous development of a short chain tool to summarize the experience and knowledge.


  • Principle that
  • Method implementation
  • We practice

Principle that

Short URL, also known as short chain, is a very short URL, from the domain name plus unique ID letter or numeric characters mixed composition, probably no more than 10 digits.

Short URL creation

Short URL creation method is as follows:

  • Get the parameter URL;
  • Query whether there is, there is a return short URL, does not exist to create a short URL;
  • When visiting short URLs, select original URLs from database as 302;

Short Website Display

Short URL from open to show the page, through the following steps:

  • Open the short URL after the request short URL server;
  • According to the unique ID to read the database site URL;
  • 302 redirect to the original website;

In the middle, visitor information (IP, device information, etc.) may be counted to collect access data for decision-making;

Short URL function

The advantages of using short URLs are as follows:

  • Short URLs look more comfortable than long ones;
  • Short URLs easily save space and database capacity;
  • Short website is more secure, avoid hackers directly attack the original station, reduce DDoS,CC attack;

Method implementation

Build table

Use the command line to connect to MySQL and create a short table.

MySQL -H 3306-u Demo -p MySQL -H 3306-u Demo -p
mysql> use demo; Database changed mysql> show tables; + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + | Tables_in_demo | + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + | goods | | user | + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - + 3 rows in the set (0.00 SEC) mysql> CREATE TABLE `short` ( `id` int(11) NOT NULL COMMENT 'id', 'sid' varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT ', 'url' varchar(1046) NOT NULL COMMENT ', 'sid' varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT ', ) ENGINE= myISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT=' short URL '; mysql> ALTER TABLE `short` CHANGE `id` `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'id'; mysql> ALTER TABLE `short` ADD PRIMARY KEY(`id`);

Methods to write

First, you install an NPM package, and then use the str10To64 and shortId methods of the package to generate the short-chain identifiers

DB method, the previous several articles have been introduced, do not know, please go back to the previous article to see

npm install xquuid
  • Create short URLs
const express = require('express'); const app = express(); const db = require('.. /model/simple'); const xqsql = require('xqsql'); const xquuid = require('xquuid'); App. Post ('/' s, async (the req, res) = > {let baseUrl = ''; let userId = 10; let url = req.body.url; let urlReg = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])? /; if (! Url) {return res.json({code: 101, MSG: 'get_fail', data: {info: "URL cannot be empty!" , } }) } if (! (urlreg.test (url))) {return res.json({code: 101, MSG: 'get_fail', data: {info: "Error!")) {return res.json({code: 101, MSG: 'get_fail', data: {info: "Error!"); , } }) } let getSql = xqsql.get('short', { type: 'one', key: 'url', ids: [url], }, 'default', 'id,url,sid'); let getSqlResult = await db(getSql); if (getSqlResult.code == 200 && == 0) { let sid = xquuid.str10To64(xquuid.shortId(userId)); let shortUrl = `${baseUrl}${sid}`; Let addParams = [{sid, url}] let addFields = [{name: 'sid', value: 'sid', isMust: true}, {name: 'sid', value: 'sid', isMust: true}, {name: 'sid', value: 'url', isMust: true }, ] let addSql = xqsql.add('short', addParams, addFields); let addSqlResult = await db(addSql); If (addSQLResult.code == 200) {return res.json({code: 200, MSG: 'get_succ', data: {info:" , url: shortUrl } }); } else { return res.json(addSqlResult); }} else {return res.json({code: 101, MSG: 'get_fail', data: {info: "url already exist!" , url: `${baseUrl}${[0].sid}` } }) } })
  • Visit short websites
Get ('/s/:sid', async (req, res) => {let sid = req.params.sid; if (! Sid) {return res.json({code: 101, MSG: 'get_fail', data: {info: "short link cannot be empty!" , } }) } let getSql = xqsql.get('short', { type: 'one', key: 'sid', ids: [sid], }, 'default', 'id,url'); let getSqlResult = await db(getSql); if (getSqlResult.code == 200 && { let url =[0].url; res.redirect(url); } else {return res.json({code: 101, MSG: 'get_fail', data: {info: "short URL does not exist!" }})}})

We practice

Long URL search

Here are two found online: 2reuDxNFwv8fQOfOtLuV5XRQkJ6xSVAfWR5lpsSwDRpA3y6CIPLGT5xW21OL2BvJxsA*TvCwkEeX4v1SWr&new=1

Postman to create

  • First open PostMan, type in a long URL, and send the request to get something like this:

Visit short websites

Copy the generated short chain for access

This is the end of the demo. If you want a shorter URL, you can purchase a domain name and resolve it to the Node server.

For example:, then buy a server, parse it, and use nginx to configure the reverse proxy.

Upstream shortSite {server weight=1; } server { listen 80; server_name; location / { proxy_pass http://shortSite/s/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; }}

Then try visiting

You can see it’s much better.

All right, that’s enough about short chains. If you have any questions, please ask them in time.