{"id":2447,"date":"2016-12-05T16:24:35","date_gmt":"2016-12-05T16:24:34","guid":{"rendered":"https:\/\/www.couchbase.com\/blog\/?p=2447"},"modified":"2022-12-15T19:41:42","modified_gmt":"2022-12-16T03:41:42","slug":"create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql","status":"publish","type":"post","link":"https:\/\/www.couchbase.com\/blog\/pt\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\/","title":{"rendered":"Criar um encurtador de URL com Node.js e Couchbase usando N1QL"},"content":{"rendered":"<p>With the boom of Twitter, SMS text messages, and other forms of short message interactions, there has been a boom in URL shortening services. For example, you can use TinyURL, Bitly, Owly, and so many others. The purpose here is to take very long URLs and make them significantly shorter for distribution in a message.<\/p>\n<p>But how do these URL shortening services work?<\/p>\n<p>We&#8217;re going to see how to create our own URL shortener <a href=\"https:\/\/www.couchbase.com\/blog\/build-a-rest-api-with-node-js-express-and-couchbase\/\">using Node.js<\/a> with Express Framework and Couchbase Server with N1QL. In our example, the short URLs will be generated using Node.js and they will be stored and accessed using Couchbase.<\/p>\n<h2 id=\"the-requirements\">The Requirements<\/h2>\n<p>There aren&#8217;t too many requirements to make this project possible. At a minimum you&#8217;ll need the following to be successful:<\/p>\n<ul>\n<li>Couchbase Server 4.1+<\/li>\n<li>Node.js 4.0+<\/li>\n<\/ul>\n<p>We need to use a version of Couchbase Server that supports N1QL queries. The Node.js version is less strict, but we will need it for serving our application and obtaining dependencies using the Node Package Manager (NPM).<\/p>\n<h2 id=\"preparing-couchbase-and-understanding-the-data-format\">Preparing Couchbase and Understanding the Data Format<\/h2>\n<p>Before we can start developing our Node.js application we need to understand the data plan as well as configure Couchbase Server to allow for N1QL queries.<\/p>\n<p>In Couchbase, the goal is to store our data in the following format:<\/p>\n<pre><code>{\r\n    \"id\": \"5Qp8oLmWX\",\r\n    \"longUrl\": \"https:\/\/www.thepolyglotdeveloper.com\/2016\/08\/using-couchbase-server-golang-web-application\/\",\r\n    \"shortUrl\": \"https:\/\/localhost:3000\/5Qp8oLmWX\"\r\n}<\/code><\/pre>\n<p>We will pass the application a long URL and generate a unique short-hash based on a piece of data. This hash will be used when constructing the short URL which will also be stored in the Couchbase document. The id of the document itself will also be that of the hash value.<\/p>\n<p>Now we need to create a Couchbase Server bucket for storing the data for our application. This bucket can be created via the Couchbase Server Administration Dashboard. Let&#8217;s call this bucket <strong>example<\/strong>.<\/p>\n<p>When querying for data we will not always be doing lookups based on the id value. This means we&#8217;ll need to create indexes on the document values to allow for N1QL queries. To keep things simple, create a simple primary index like the following:<\/p>\n<pre><code>CREATE PRIMARY INDEX ON `example` USING GSI;<\/code><\/pre>\n<p>This query can be executed using the Couchbase Server Query Workbench (Enterprise Edition) or the Couchbase Shell known as CBQ. The index won&#8217;t be the quickest because it is so general, but it will accomplish the needs of our very simple application.<\/p>\n<h2 id=\"developing-the-node.js-url-shortener-application-with-express-framework\">Developing the Node.js URL Shortener Application with Express Framework<\/h2>\n<p>With the database properly configured we can worry about the code behind the application. This application will be heavily dependent on Express Framework and Couchbase, but also a hashing library known as Hashids.<\/p>\n<h3 id=\"creating-the-project-with-the-dependencies\">Creating the Project with the Dependencies<\/h3>\n<p>Let&#8217;s create a fresh Node.js project from the Command Prompt (Windows) or Terminal (Mac and Linux):<\/p>\n<pre><code>npm init --y<\/code><\/pre>\n<p>The above command will create a <strong>package.json<\/strong> file wherever you&#8217;re currently navigated to via your command line. So hopefully you&#8217;re within a fresh directory.<\/p>\n<p>Now we need to install the project dependencies. Execute the following from the command line:<\/p>\n<pre><code>npm install couchbase body-parser express hashids --save<\/code><\/pre>\n<p>At this point we can worry about the JavaScript development.<\/p>\n<h3 id=\"bootstrapping-the-node.js-application\">Bootstrapping the Node.js Application<\/h3>\n<p>Too keep this project simple and easy to understand, we&#8217;re going to keep all application logic in a single file. In a production application you&#8217;ll probably want to break it up for cleanliness and maintainability, but for this example we&#8217;re going to be fine.<\/p>\n<p>Create a file called <strong>app.js<\/strong> at the root of your project directory. In this file, the first thing we&#8217;re going to do is import the installed dependencies like so:<\/p>\n<pre><code>var Couchbase = require(\"couchbase\");\r\nvar Express = require(\"express\");\r\nvar BodyParser = require(\"body-parser\");\r\nvar Hashids = require(\"hashids\");<\/code><\/pre>\n<p>Because I&#8217;ve yet to explain the <code>body-parser<\/code> dependency, I&#8217;ll explain it now. This dependency allows us to make requests that contain a body. For example, when making POST or PUT requests it is common to include a JSON body rather than URL parameters or query parameters.<\/p>\n<p>With the dependencies imported we need to initialize Express Framework and the Couchbase N1QL Engine:<\/p>\n<pre><code>var app = Express();\r\nvar N1qlQuery = Couchbase.N1qlQuery;<\/code><\/pre>\n<p>While we&#8217;ve imported the <code>body-parser<\/code> plugin, we&#8217;ve yet to initialize it. We want to be able to accept JSON and URL encoded values, so we need to configure the dependency like so:<\/p>\n<pre><code>app.use(BodyParser.json());\r\napp.use(BodyParser.urlencoded({ extended: true }));<\/code><\/pre>\n<p>At this point all of our dependencies are initialized. It would be a good idea now to establish a connection to our Couchbase Server cluster and application bucket. This can be accomplished with the following line:<\/p>\n<pre><code>var bucket = (new Couchbase.Cluster(\"couchbase:\/\/localhost\")).openBucket(\"example\");<\/code><\/pre>\n<p>Finally let&#8217;s start serving our Node.js application with the following:<\/p>\n<pre><code>var server = app.listen(3000, function() {\r\n    console.log(\"Listening on port %s...\", server.address().port);\r\n});<\/code><\/pre>\n<p>You&#8217;re probably realizing that we haven&#8217;t really added any logic. You&#8217;re correct in that realization as we&#8217;ve only really bootstrapped our application up until now.<\/p>\n<h3 id=\"creating-the-url-shortener-application-logic\">Creating the URL Shortener Application Logic<\/h3>\n<p>What we want to do now is create some RESTful API endpoints. We&#8217;re going to create following endpoints:<\/p>\n<pre><code>\/\r\n\/expand\r\n\/create<\/code><\/pre>\n<p>The <strong>root<\/strong> endpoint will be used for navigating to a long URL that is masked behind a short URL. The <strong>\/expand<\/strong> endpoint will take a short URL and reveal the long URL without navigating to it and the <strong>\/create<\/strong> endpoint will take a long URL and create a short URL in the database.<\/p>\n<p>Starting with the <strong>\/create<\/strong> and probably most complicated endpoint, we have the following:<\/p>\n<pre><code>app.post(\"\/create\", function(req, res) {\r\n    if(!req.body.longUrl) {\r\n        return res.status(400).send({\"status\": \"error\", \"message\": \"A long URL is required\"});\r\n    }\r\n    bucket.query(N1qlQuery.fromString(\"SELECT `\" + bucket._name + \"`.* FROM `\" + bucket._name + \"` WHERE longUrl = $1\"), [req.body.longUrl], function(error, result) {\r\n        if(error) {\r\n            return res.status(400).send(error);\r\n        }\r\n        if(result.length == 0) {\r\n            var hashids = new Hashids();\r\n            var response = {\r\n                id: hashids.encode((new Date).getTime()),\r\n                longUrl: req.body.longUrl\r\n            }\r\n            response.shortUrl = \"https:\/\/localhost:3000\/\" + response.id;\r\n            bucket.insert(response.id, response, function(error, result) {\r\n                if(error) {\r\n                    return res.status(400).send(error);\r\n                }\r\n                res.send(response);\r\n            });\r\n        } else {\r\n            res.send(result[0]);\r\n        }\r\n    });\r\n});<\/code><\/pre>\n<p>This endpoint is a POST request that expects a JSON body. If the <code>longUrl<\/code> JSON property does not exist, an error will be returned to the user.<\/p>\n<p>Before we actually create the short URL, we want to make sure one hasn&#8217;t already been created. We do this because we want one short URL for every one long URL. We can accomplish this by creating a parameterized N1QL query based on the <code>longUrl<\/code> property. If the response contains a document, we&#8217;ll return it because the document already exists. If the response does not have a document, we need to create one.<\/p>\n<p>Using the <code>hashids<\/code> dependency we can create a hash based on the timestamp and use that as our id and our short URL. After inserting this new document we can return it back to the user.<\/p>\n<p>Now let&#8217;s take a look at how to expand those short URLs.<\/p>\n<pre><code>app.get(\"\/expand\", function(req, res) {\r\n    if(!req.query.shortUrl) {\r\n        return res.status(400).send({\"status\": \"error\", \"message\": \"A short URL is required\"});\r\n    }\r\n    bucket.query(N1qlQuery.fromString(\"SELECT `\" + bucket._name + \"`.* FROM `\" + bucket._name + \"` WHERE shortUrl = $1\"), [req.query.shortUrl], function(error, result) {\r\n        if(error) {\r\n            return res.status(400).send(error);\r\n        }\r\n        res.send(result.length &gt; 0 ? result[0] : {});\r\n    });\r\n});<\/code><\/pre>\n<p>The above code uses a similar concept to the <strong>\/create<\/strong> endpoint. We take a <code>shortUrl<\/code> value and query for it using N1QL. If found, we can return the long URL with the response.<\/p>\n<p>Finally we can worry about navigation.<\/p>\n<pre><code>app.get(\"\/:id\", function(req, res) {\r\n    if(!req.params.id) {\r\n        return res.status(400).send({\"status\": \"error\", \"message\": \"An id is required\"});\r\n    }\r\n    bucket.get(req.params.id, function(error, result) {\r\n        if(error) {\r\n            return res.status(400).send(error);\r\n        }\r\n        res.redirect(result.value.longUrl);\r\n    })\r\n});<\/code><\/pre>\n<p>Remember, our short URLs are in the format of <strong>https:\/\/localhost:3000\/5Qp8oLmWX<\/strong> which is the same location as our API service. What this means is that <strong>5Qp8oLmWX<\/strong> is just a URL parameter to our <strong>root<\/strong> endpoint.<\/p>\n<p>With the id we can do a document lookup based on the key value. If successful we&#8217;ll have the document that is currently stored.<\/p>\n<h3 id=\"the-full-application-source-code\">The Full Application Source Code<\/h3>\n<p>In case you wanted to see the full source code to the application we had just created, it can be found below.<\/p>\n<pre><code>var Couchbase = require(\"couchbase\");\r\nvar Express = require(\"express\");\r\nvar BodyParser = require(\"body-parser\");\r\nvar Hashids = require(\"hashids\");\r\n\r\nvar app = Express();\r\nvar N1qlQuery = Couchbase.N1qlQuery;\r\n\r\napp.use(BodyParser.json());\r\napp.use(BodyParser.urlencoded({ extended: true }));\r\n\r\nvar bucket = (new Couchbase.Cluster(\"couchbase:\/\/localhost\")).openBucket(\"example\");\r\n\r\napp.get(\"\/expand\", function(req, res) {\r\n    if(!req.query.shortUrl) {\r\n        return res.status(400).send({\"status\": \"error\", \"message\": \"A short URL is required\"});\r\n    }\r\n    bucket.query(N1qlQuery.fromString(\"SELECT `\" + bucket._name + \"`.* FROM `\" + bucket._name + \"` WHERE shortUrl = $1\"), [req.query.shortUrl], function(error, result) {\r\n        if(error) {\r\n            return res.status(400).send(error);\r\n        }\r\n        res.send(result.length &gt; 0 ? result[0] : {});\r\n    });\r\n});\r\n\r\napp.post(\"\/create\", function(req, res) {\r\n    if(!req.body.longUrl) {\r\n        return res.status(400).send({\"status\": \"error\", \"message\": \"A long URL is required\"});\r\n    }\r\n    bucket.query(N1qlQuery.fromString(\"SELECT `\" + bucket._name + \"`.* FROM `\" + bucket._name + \"` WHERE longUrl = $1\"), [req.body.longUrl], function(error, result) {\r\n        if(error) {\r\n            return res.status(400).send(error);\r\n        }\r\n        if(result.length == 0) {\r\n            var hashids = new Hashids();\r\n            var response = {\r\n                id: hashids.encode((new Date).getTime()),\r\n                longUrl: req.body.longUrl\r\n            }\r\n            response.shortUrl = \"https:\/\/localhost:3000\/\" + response.id;\r\n            bucket.insert(response.id, response, function(error, result) {\r\n                if(error) {\r\n                    return res.status(400).send(error);\r\n                }\r\n                res.send(response);\r\n            });\r\n        } else {\r\n            res.send(result[0]);\r\n        }\r\n    });\r\n});\r\n\r\napp.get(\"\/:id\", function(req, res) {\r\n    if(!req.params.id) {\r\n        return res.status(400).send({\"status\": \"error\", \"message\": \"An id is required\"});\r\n    }\r\n    bucket.get(req.params.id, function(error, result) {\r\n        if(error) {\r\n            return res.status(400).send(error);\r\n        }\r\n        res.redirect(result.value.longUrl);\r\n    })\r\n});\r\n\r\nvar server = app.listen(3000, function() {\r\n    console.log(\"Listening on port %s...\", server.address().port);\r\n});<\/code><\/pre>\n<p>There are probably many optimizations that can be done, but we cared more about the logic in making this a successful project.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>You just saw how to create a very basic URL shortener using Node.js for the application logic, Couchbase Server as the NoSQL database, and N1QL as the query technology.<\/p>\n<p>If you wanted to take this to the next level you could keep track of analytic information. For example, if someone navigates to the <strong>root<\/strong> endpoint, increase a counter, or store the browser agent. Simple things to add a cool-factor to the URL shortener application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With the boom of Twitter, SMS text messages, and other forms of short message interactions, there has been a boom in URL shortening services. For example, you can use TinyURL, Bitly, Owly, and so many others. The purpose here is [&hellip;]<\/p>\n","protected":false},"author":63,"featured_media":13873,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[1815,1822,1812],"tags":[1768,1767],"ppma_author":[9032],"class_list":["post-2447","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-best-practices-and-tutorials","category-node-js","category-n1ql-query","tag-short-hash","tag-url-shortener"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Creating URL Shortener with Node.js and Couchbase using N1QL<\/title>\n<meta name=\"description\" content=\"How to create a very basic URL shortener using Node.js for the application logic, Couchbase Server as the NoSQL database, and N1QL as the query technology.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.couchbase.com\/blog\/pt\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\/\" \/>\n<meta property=\"og:locale\" content=\"pt_BR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create a URL Shortener with Node.js and Couchbase using N1QL\" \/>\n<meta property=\"og:description\" content=\"How to create a very basic URL shortener using Node.js for the application logic, Couchbase Server as the NoSQL database, and N1QL as the query technology.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.couchbase.com\/blog\/pt\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\/\" \/>\n<meta property=\"og:site_name\" content=\"The Couchbase Blog\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/thepolyglotdeveloper\" \/>\n<meta property=\"article:published_time\" content=\"2016-12-05T16:24:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-12-16T03:41:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2022\/11\/couchbase-nosql-dbaas.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1800\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Nic Raboy, Developer Advocate, Couchbase\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@nraboy\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nic Raboy, Developer Advocate, Couchbase\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\\\/\"},\"author\":{\"name\":\"Nic Raboy, Developer Advocate, Couchbase\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/#\\\/schema\\\/person\\\/bb545ebe83bb2d12f91095811d0a72e1\"},\"headline\":\"Create a URL Shortener with Node.js and Couchbase using N1QL\",\"datePublished\":\"2016-12-05T16:24:34+00:00\",\"dateModified\":\"2022-12-16T03:41:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\\\/\"},\"wordCount\":1252,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/wp-content\\\/uploads\\\/sites\\\/1\\\/2022\\\/11\\\/couchbase-nosql-dbaas.png\",\"keywords\":[\"short hash\",\"url shortener\"],\"articleSection\":[\"Best Practices and Tutorials\",\"Node.js\",\"SQL++ \\\/ N1QL Query\"],\"inLanguage\":\"pt-BR\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\\\/\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\\\/\",\"name\":\"Creating URL Shortener with Node.js and Couchbase using N1QL\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/wp-content\\\/uploads\\\/sites\\\/1\\\/2022\\\/11\\\/couchbase-nosql-dbaas.png\",\"datePublished\":\"2016-12-05T16:24:34+00:00\",\"dateModified\":\"2022-12-16T03:41:42+00:00\",\"description\":\"How to create a very basic URL shortener using Node.js for the application logic, Couchbase Server as the NoSQL database, and N1QL as the query technology.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\\\/#breadcrumb\"},\"inLanguage\":\"pt-BR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/wp-content\\\/uploads\\\/sites\\\/1\\\/2022\\\/11\\\/couchbase-nosql-dbaas.png\",\"contentUrl\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/wp-content\\\/uploads\\\/sites\\\/1\\\/2022\\\/11\\\/couchbase-nosql-dbaas.png\",\"width\":1800,\"height\":630},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create a URL Shortener with Node.js and Couchbase using N1QL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/\",\"name\":\"The Couchbase Blog\",\"description\":\"Couchbase, the NoSQL Database\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"pt-BR\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/#organization\",\"name\":\"The Couchbase Blog\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/04\\\/admin-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/04\\\/admin-logo.png\",\"width\":218,\"height\":34,\"caption\":\"The Couchbase Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/#\\\/schema\\\/person\\\/bb545ebe83bb2d12f91095811d0a72e1\",\"name\":\"Nic Raboy, Developer Advocate, Couchbase\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/bedeb68368d4681aca4c74fe5f697f0c423b80d498ec50fd915ba018b72c101f?s=96&d=mm&r=g8863514d8bed0cf6080f23db40e00354\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/bedeb68368d4681aca4c74fe5f697f0c423b80d498ec50fd915ba018b72c101f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/bedeb68368d4681aca4c74fe5f697f0c423b80d498ec50fd915ba018b72c101f?s=96&d=mm&r=g\",\"caption\":\"Nic Raboy, Developer Advocate, Couchbase\"},\"description\":\"Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.\",\"sameAs\":[\"https:\\\/\\\/www.thepolyglotdeveloper.com\",\"https:\\\/\\\/www.facebook.com\\\/thepolyglotdeveloper\",\"https:\\\/\\\/x.com\\\/nraboy\"],\"url\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/pt\\\/author\\\/nic-raboy-2\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Creating URL Shortener with Node.js and Couchbase using N1QL","description":"Como criar um encurtador de URL muito b\u00e1sico usando o Node.js para a l\u00f3gica do aplicativo, o Couchbase Server como banco de dados NoSQL e o N1QL como tecnologia de consulta.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.couchbase.com\/blog\/pt\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\/","og_locale":"pt_BR","og_type":"article","og_title":"Create a URL Shortener with Node.js and Couchbase using N1QL","og_description":"How to create a very basic URL shortener using Node.js for the application logic, Couchbase Server as the NoSQL database, and N1QL as the query technology.","og_url":"https:\/\/www.couchbase.com\/blog\/pt\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\/","og_site_name":"The Couchbase Blog","article_author":"https:\/\/www.facebook.com\/thepolyglotdeveloper","article_published_time":"2016-12-05T16:24:34+00:00","article_modified_time":"2022-12-16T03:41:42+00:00","og_image":[{"width":1800,"height":630,"url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2022\/11\/couchbase-nosql-dbaas.png","type":"image\/png"}],"author":"Nic Raboy, Developer Advocate, Couchbase","twitter_card":"summary_large_image","twitter_creator":"@nraboy","twitter_misc":{"Written by":"Nic Raboy, Developer Advocate, Couchbase","Est. reading time":"8 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.couchbase.com\/blog\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\/#article","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\/"},"author":{"name":"Nic Raboy, Developer Advocate, Couchbase","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/bb545ebe83bb2d12f91095811d0a72e1"},"headline":"Create a URL Shortener with Node.js and Couchbase using N1QL","datePublished":"2016-12-05T16:24:34+00:00","dateModified":"2022-12-16T03:41:42+00:00","mainEntityOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\/"},"wordCount":1252,"commentCount":0,"publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png","keywords":["short hash","url shortener"],"articleSection":["Best Practices and Tutorials","Node.js","SQL++ \/ N1QL Query"],"inLanguage":"pt-BR","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.couchbase.com\/blog\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.couchbase.com\/blog\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\/","url":"https:\/\/www.couchbase.com\/blog\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\/","name":"Creating URL Shortener with Node.js and Couchbase using N1QL","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\/#primaryimage"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png","datePublished":"2016-12-05T16:24:34+00:00","dateModified":"2022-12-16T03:41:42+00:00","description":"Como criar um encurtador de URL muito b\u00e1sico usando o Node.js para a l\u00f3gica do aplicativo, o Couchbase Server como banco de dados NoSQL e o N1QL como tecnologia de consulta.","breadcrumb":{"@id":"https:\/\/www.couchbase.com\/blog\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\/#breadcrumb"},"inLanguage":"pt-BR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.couchbase.com\/blog\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\/"]}]},{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/www.couchbase.com\/blog\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\/#primaryimage","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png","width":1800,"height":630},{"@type":"BreadcrumbList","@id":"https:\/\/www.couchbase.com\/blog\/create-a-url-shortener-with-nodejs-and-couchbase-using-n1ql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.couchbase.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Create a URL Shortener with Node.js and Couchbase using N1QL"}]},{"@type":"WebSite","@id":"https:\/\/www.couchbase.com\/blog\/#website","url":"https:\/\/www.couchbase.com\/blog\/","name":"Blog do Couchbase","description":"Couchbase, o banco de dados NoSQL","publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.couchbase.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"pt-BR"},{"@type":"Organization","@id":"https:\/\/www.couchbase.com\/blog\/#organization","name":"Blog do Couchbase","url":"https:\/\/www.couchbase.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png","width":218,"height":34,"caption":"The Couchbase Blog"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/bb545ebe83bb2d12f91095811d0a72e1","name":"Nic Raboy, defensor dos desenvolvedores, Couchbase","image":{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/secure.gravatar.com\/avatar\/bedeb68368d4681aca4c74fe5f697f0c423b80d498ec50fd915ba018b72c101f?s=96&d=mm&r=g8863514d8bed0cf6080f23db40e00354","url":"https:\/\/secure.gravatar.com\/avatar\/bedeb68368d4681aca4c74fe5f697f0c423b80d498ec50fd915ba018b72c101f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/bedeb68368d4681aca4c74fe5f697f0c423b80d498ec50fd915ba018b72c101f?s=96&d=mm&r=g","caption":"Nic Raboy, Developer Advocate, Couchbase"},"description":"Nic Raboy \u00e9 um defensor das modernas tecnologias de desenvolvimento m\u00f3vel e da Web. Ele tem experi\u00eancia em Java, JavaScript, Golang e uma variedade de estruturas, como Angular, NativeScript e Apache Cordova. Nic escreve sobre suas experi\u00eancias de desenvolvimento relacionadas a tornar o desenvolvimento m\u00f3vel e da Web mais f\u00e1cil de entender.","sameAs":["https:\/\/www.thepolyglotdeveloper.com","https:\/\/www.facebook.com\/thepolyglotdeveloper","https:\/\/x.com\/nraboy"],"url":"https:\/\/www.couchbase.com\/blog\/pt\/author\/nic-raboy-2\/"}]}},"acf":[],"authors":[{"term_id":9032,"user_id":63,"is_guest":0,"slug":"nic-raboy-2","display_name":"Nic Raboy, Developer Advocate, Couchbase","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/bedeb68368d4681aca4c74fe5f697f0c423b80d498ec50fd915ba018b72c101f?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/posts\/2447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/comments?post=2447"}],"version-history":[{"count":0,"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/posts\/2447\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/media\/13873"}],"wp:attachment":[{"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/media?parent=2447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/categories?post=2447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/tags?post=2447"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/ppma_author?post=2447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}