Free

URL Shortener

301 or 302 is a decision you make once and can never take back.

  • Node.js
  • HTTP
  • JSON

About this project

Paste a long URL, get a short code, and visiting the code redirects. A small web page, a JSON API, and a hit counter per link. Built on Node's own http module — no Express, no database, no npm install.

The idea it is built around is the redirect status. A 302 says "go there for now" and the browser comes back to you every time. A 301 says "go there from now on, and stop asking" — and browsers cache that until the user clears their browsing data. Send a 301 and two things follow: you can never change where that link points for anyone who has already clicked it, and your hit counter stops counting, because the browser no longer asks. The default here is 302, and a --permanent flag lets you switch and watch the counter stop moving.

The second theme is that an open redirect is a real vulnerability. A redirector that will send visitors anywhere is a tool for other people's phishing, because the link starts with a domain the victim trusts. Targets are validated against a scheme whitelist before they are stored, including the case most people misread — the username before an @ sign.

What you will learn

  • Choose between 301 and 302 knowing what each costs, and what each means to a search engine
  • Prevent an open redirect by validating the target against a scheme whitelist
  • Generate short codes with crypto.randomBytes, and know why a sequential id leaks
  • Write a file atomically — temp file, fsync, rename — so a crash cannot corrupt it
  • Build an HTTP server on Node's own modules, so you can see what a framework hides

Before you start

  • JavaScript basics — functions, objects and callbacks
  • Node 18 or newer. Nothing to install: built-in modules only.

New to Node.js? The free Node.js course covers everything this project assumes.

Academic integrity notice

These projects are provided for learning, experimentation and reference. Understand the code, modify it for your own requirements, and acknowledge external work according to your institution’s academic policies. Priodemy does not supply work to be submitted as your own.