2018-10-25 20:29:56 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: bd7158d8c443edefaeb5bd0e
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: URL Shortener Microservice
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								challengeType: 4
							 
						 
					
						
							
								
									
										
										
										
											2019-08-05 09:17:33 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 301509
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								dashedName: url-shortener-microservice
							 
						 
					
						
							
								
									
										
										
										
											2018-10-25 20:29:56 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --description--
  
						 
					
						
							
								
									
										
										
										
											2020-11-02 03:54:56 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Build a full stack JavaScript app that is functionally similar to this: < https: / / url-shortener-microservice . freecodecamp . rocks / > . Working on this project will involve you writing your code using one of the following methods:
							 
						 
					
						
							
								
									
										
										
										
											2020-11-20 12:02:31 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-    Clone [this GitHub repo ](https://github.com/freeCodeCamp/boilerplate-project-urlshortener/ ) and complete your project locally. 
						 
					
						
							
								
									
										
										
										
											2021-04-29 06:13:38 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-    Use [our Replit starter project ](https://replit.com/github/freeCodeCamp/boilerplate-project-urlshortener ) to complete your project. 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-    Use a site builder of your choice to complete the project. Be sure to incorporate all the files from our GitHub repo. 
						 
					
						
							
								
									
										
										
										
											2020-11-20 12:02:31 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								When you are done, make sure a working demo of your project is hosted somewhere public. Then submit the URL to it in the `Solution Link`  field. Optionally, also submit a link to your projects source code in the `GitHub Link`  field.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-25 20:29:56 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --instructions--
  
						 
					
						
							
								
									
										
										
										
											2018-10-25 20:29:56 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-03 15:06:07 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								**HINT:** Do not forget to use a body parsing middleware to handle the POST requests. Also, you can use the function `dns.lookup(host, cb)`  from the `dns`  core module to verify a submitted URL.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --hints--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You should provide your own project, not the example URL.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								(getUserInput) => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    !/.*\/url-shortener-microservice\.freecodecamp\.rocks/.test(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      getUserInput('url')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
									
										
										
										
											2018-10-25 20:29:56 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-13 20:28:27 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You can POST a URL to `/api/shorturl`  and get a JSON response with `original_url`  and `short_url`  properties. Here's an example: `{ original_url : 'https://freeCodeCamp.org', short_url : 1}` 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-25 20:29:56 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async (getUserInput) => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const url = getUserInput('url');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const urlVariable = Date.now();
							 
						 
					
						
							
								
									
										
										
										
											2021-02-25 12:04:09 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const fullUrl = `${url}/?v=${urlVariable}` 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-13 20:28:27 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const res = await fetch(url + '/api/shorturl', {
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    method: 'POST',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
							 
						 
					
						
							
								
									
										
										
										
											2021-02-25 12:04:09 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    body: `url=${fullUrl}` 
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  if (res.ok) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const { short_url, original_url } = await res.json();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    assert.isNotNull(short_url);
							 
						 
					
						
							
								
									
										
										
										
											2021-02-25 12:04:09 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    assert.strictEqual(original_url, `${url}/?v=${urlVariable}` );
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    throw new Error(`${res.status} ${res.statusText}` );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2020-11-02 03:54:56 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								When you visit `/api/shorturl/<short_url>` , you will be redirected to the original URL.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-25 20:29:56 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async (getUserInput) => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const url = getUserInput('url');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const urlVariable = Date.now();
							 
						 
					
						
							
								
									
										
										
										
											2021-02-25 12:04:09 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const fullUrl = `${url}/?v=${urlVariable}` 
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  let shortenedUrlVariable;
							 
						 
					
						
							
								
									
										
										
										
											2021-04-13 20:28:27 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const postResponse = await fetch(url + '/api/shorturl', {
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    method: 'POST',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
							 
						 
					
						
							
								
									
										
										
										
											2021-02-25 12:04:09 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    body: `url=${fullUrl}` 
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  if (postResponse.ok) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const { short_url } = await postResponse.json();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    shortenedUrlVariable = short_url;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    throw new Error(`${postResponse.status} ${postResponse.statusText}` );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const getResponse = await fetch(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    url + '/api/shorturl/' + shortenedUrlVariable
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  if (getResponse) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const { redirected, url } = getResponse;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    assert.isTrue(redirected);
							 
						 
					
						
							
								
									
										
										
										
											2021-02-25 12:04:09 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    assert.strictEqual(url,fullUrl);
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    throw new Error(`${getResponse.status} ${getResponse.statusText}` );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If you pass an invalid URL that doesn't follow the valid `http://www.example.com`  format, the JSON response will contain `{ error: 'invalid url' }` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async (getUserInput) => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const url = getUserInput('url');
							 
						 
					
						
							
								
									
										
										
										
											2021-04-13 20:28:27 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const res = await fetch(url + '/api/shorturl', {
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    method: 'POST',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    body: `url=ftp:/john-doe.org` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  if (res.ok) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const { error } = await res.json();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    assert.isNotNull(error);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    assert.strictEqual(error.toLowerCase(), 'invalid url');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    throw new Error(`${res.status} ${res.statusText}` );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-25 20:29:56 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --solutions--
  
						 
					
						
							
								
									
										
										
										
											2018-10-25 20:29:56 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-10-24 10:08:13 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								/**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Backend challenges don't need solutions, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  because they would need to be tested against a full working project. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Please check our contributing guidelines to learn more.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*/
							 
						 
					
						
							
								
									
										
										
										
											2018-10-25 20:29:56 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```