DevConnectDevConnect
Sign up · Log in
← back to the feed
0
showcase

Shipped: Short link service

What I built: I built a short link service in Node.js. You send it a long URL and it gives back a short code. When someone opens the short code, it sends them to the original URL and counts that visit. It has three endpoints: - POST /links creates a short link. - GET /:code follows the short link. - GET /links/:code/stats shows how many times it was followed. How to check it: Start the server with npm start. In a second terminal, run npm run demo. The script runs every check below and prints the status code next to each one. You can also run npm test to see all 32 tests pass. 1. Creating, following and counting each work. The demo creates a link and gets 201. It follows the link twice, then checks the stats and sees a count of 2. 2. 301 or 302 is chosen on purpose. I used 302. Browsers remember a 301, so repeat visits would skip my server and never be counted. A 301 also can’t be undone once a browser has saved it. The reason is written in the README under “302, not 301”. 3. An unknown code returns 404. The demo opens a code that doesn’t exist and gets a 404 with an error message, not an empty 200. 4. A malformed URL is rejected with 400 before storage. The demo sends “not a url” and a javascript: link. Both get a 400 and the message names the field, for example {"error": "url must use http or https", "field": "url"}. The checks run before anything is saved, and a test confirms nothing was stored. Broken JSON also gets a 400, not a 500. 5. The same URL submitted twice does not create two codes. The demo sends the same URL twice. The first time gets 201 and the second gets 200 with the same code. A test also sends 20 identical requests at once and checks that only one link is stored. The README has the full list of endpoints, the error codes and the known limits.
Add a comment
0/2000