
Tall is a dependency-free, promise-based library that allows you to easily resolve a short URL and get its corresponding long URL.
#TINYURL EXPANDER CODE#
You can easily adapt the code above to work with your specific use case, but if you want a ready-made and easy to use URL-unshortener NPM library you can trust a library I recently published called tall. A Promise-based, dependency free, Node.js library for expanding short URLs

In case we are not dealing with a short URL, this script will print back the original URL passed to the function. By doing so we have an opportunity to catch a redirect response in our callback and so we can read its Location header which will represent the expanded URL. By default, this library follows redirects so we need to explicitly disable this behaviour by specifying the option followRedirect: false. Var request = require ( 'request' ) var uri = '' request ( )įor simplicity, in this script, we are using the request module to perform the HTTP request. Now that we know how short URL services work, it shouldn’t be hard to create a Node.js script that is capable of resolving any type of short URL. Simple enough, right? Resolving short URLs in Node.js This is what happens when a user through a browser deals with a short URL, everything happens behind the scenes and the user won’t even notice that there was an exchange with a short URL server. Se o link utilizar um serviço de encurtamento de URL como TinyURL ou Bitly.

Finally, if a match is found, the web server responds with a redirect message, sending the browser of the user to the corresponding long URL. URL shorteners have to expose a web server which will be reached through all the domains mapped (in fact, most of the URL shortener services - like Rebrandly - supports different domains or even the possibility to associate your own branded domain).įor every request that satisfies the short URL format, domain and id are extracted and a query is executed against the database to search for the corresponding long URL.

Note that in this example the table supports multiple domains and that the pair domain- id must be unique in the table.
