WeCodeAcademy

Step 1: Plan the service

In this project you build a URL shortener like the ones behind short links on social media: give it a long web address, get back a short code; visit the short code and you are redirected to the long address. Along the way you meet random ids, input validation, redirects, click statistics, rate limiting, a SQL design and tests. You use only Node's built-in modules, and every example's output is real.

The behaviour: POST /links with { "url": ... } creates a short link (201); GET /<id> redirects to the original (302); GET /stats/<id> reports how often it was used. Security matters here: a shortener sends people to addresses someone else typed, so it must refuse dangerous targets (like javascript: links) and must not let one client flood the service.

ExampleNode.js
const spec = [
  ["POST", "/links", "create a short link", "201 or 400"],
  ["GET", "/:id", "redirect to the original", "302 or 404"],
  ["GET", "/stats/:id", "show clicks and details", "200 or 404"],
];
for (const [method, path, what, codes] of spec) {
  console.log(method.padEnd(5), path.padEnd(11), what.padEnd(26), codes);
}
Output
POST  /links      create a short link        201 or 400
GET   /:id        redirect to the original   302 or 404
GET   /stats/:id  show clicks and details    200 or 404
Quick check

Which status code does a redirect use?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.