2018-10-25 20:29:56 +02:00
---
id: bd7158d8c443edefaeb5bd0e
2021-10-24 20:56:40 -07:00
title: Microservicio acortador de URL
2018-10-25 20:29:56 +02:00
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
2021-10-24 20:56:40 -07:00
Construye una aplicación full stack de JavaScript que sea funcionalmente similar a esta: < https: / / url-shortener-microservice . freecodecamp . rocks / > . Trabajar en este proyecto implicará escribir tu código utilizando uno de los siguientes métodos:
2020-11-20 12:02:31 -08:00
2021-10-24 20:56:40 -07:00
- Clona [este repositorio de GitHub ](https://github.com/freeCodeCamp/boilerplate-project-urlshortener/ ) y completa tu proyecto localmente.
- Usa [nuestro proyecto de inicio en Replit ](https://replit.com/github/freeCodeCamp/boilerplate-project-urlshortener ) para completar tu proyecto.
- Utiliza un constructor de sitios de tu elección para completar el proyecto. Asegúrate de incorporar todos los archivos de nuestro repositorio de GitHub.
2020-11-20 12:02:31 -08:00
2021-10-24 20:56:40 -07:00
Cuando hayas terminado, asegúrate de que un demo funcional de tu proyecto esté alojado en algún lugar público. Luego, envía la URL en el campo `Solution Link` . Opcionalmente, también envía un enlace al código fuente de tu proyecto en el campo `GitHub Link` .
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
2021-10-24 20:56:40 -07:00
**NOTA:** No olvides usar un middleware para manejar las peticiones POST. También, puedes usar la función `dns.lookup(host, cb)` desde el módulo principal `dns` para verificar una URL enviada.
2020-11-03 15:06:07 +00:00
2020-11-27 19:02:05 +01:00
# --hints--
2021-10-24 20:56:40 -07:00
Debes proporcionar tu propio proyecto, no la URL de ejemplo.
2020-11-27 19:02:05 +01:00
```js
(getUserInput) => {
assert(
!/.*\/url-shortener-microservice\.freecodecamp\.rocks/.test(
getUserInput('url')
)
);
};
2018-10-25 20:29:56 +02:00
```
2021-10-24 20:56:40 -07:00
Puedes publicar una URL en `/api/shorturl` y obtener una respuesta JSON con las propiedades `original_url` y `short_url` . Aquí hay un ejemplo: `{ 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
2021-10-24 20:56:40 -07:00
Cuando visitas `/api/shorturl/<short_url>` , serás redirigido a la URL original.
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}` );
}
};
```
2021-10-24 20:56:40 -07:00
Si pasas una URL inválida que no sigue el formato válido `http://www.example.com` , la respuesta JSON contendrá `{ error: 'invalid url' }`
2020-11-27 19:02:05 +01:00
```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' },
2022-03-09 20:59:59 +05:30
body: `url=ftp:/john-doe.invalidTLD`
2020-11-27 19:02:05 +01:00
});
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
```