Files
2022-03-09 07:29:59 -08:00

4.1 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
bd7158d8c443edefaeb5bd0e Microsserviço redutor de URL 4 301509 url-shortener-microservice

--description--

Crie um aplicativo full stack em JavaScript que seja funcionalmente semelhante a este: https://url-shortener-microservice.freecodecamp.rocks/. Trabalhar nesse projeto vai fazer com que você escreva seu código usando um dos seguintes métodos:

Quando terminar, certifique-se de que uma demonstração funcional do seu projeto está hospedada em algum lugar público. Em seguida, envie o URL para ela no campo Solution Link. Como opção, envie também um link para o código-fonte do projeto no campo GitHub Link.

--instructions--

DICA: não se esqueça de usar um middleware de análise do body para tratar as solicitações de POST. Você também pode usar a função dns.lookup(host, cb) do módulo central do dns para verificar um URL enviado.

--hints--

Você deve fornecer seu próprio projeto, não o exemplo de URL.

(getUserInput) => {
  assert(
    !/.*\/url-shortener-microservice\.freecodecamp\.rocks/.test(
      getUserInput('url')
    )
  );
};

Você pode postar um URL para /api/shorturl e obter uma resposta em JSON com as propriedades original_url e short_url. Aqui está um exemplo: { original_url : 'https://freeCodeCamp.org', short_url : 1}

async (getUserInput) => {
  const url = getUserInput('url');
  const urlVariable = Date.now();
  const fullUrl = `${url}/?v=${urlVariable}`
  const res = await fetch(url + '/api/shorturl', {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: `url=${fullUrl}`
  });
  if (res.ok) {
    const { short_url, original_url } = await res.json();
    assert.isNotNull(short_url);
    assert.strictEqual(original_url, `${url}/?v=${urlVariable}`);
  } else {
    throw new Error(`${res.status} ${res.statusText}`);
  }
};

Quando você visitar /api/shorturl/<short_url>, será redirecionado para o URL original.

async (getUserInput) => {
  const url = getUserInput('url');
  const urlVariable = Date.now();
  const fullUrl = `${url}/?v=${urlVariable}`
  let shortenedUrlVariable;
  const postResponse = await fetch(url + '/api/shorturl', {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: `url=${fullUrl}`
  });
  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);
    assert.strictEqual(url,fullUrl);
  } else {
    throw new Error(`${getResponse.status} ${getResponse.statusText}`);
  }
};

Se você passar um URL inválido que não siga o formato válido http://www.example.com, a resposta em JSON conterá { error: 'invalid url' }

async (getUserInput) => {
  const url = getUserInput('url');
  const res = await fetch(url + '/api/shorturl', {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: `url=ftp:/john-doe.invalidTLD`
  });
  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}`);
  }
};

--solutions--

/**
  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.
*/