URL Shortener REST API Bug
Spot the REST design mistake in a simple URL shortener API built with Express.
Codejavascript
const express = require('express');
const app = express();
app.use(express.json());
const store = {};
// Create a short URL
app.get('/urls', (req, res) => {
const { longUrl } = req.body;
const code = Math.random().toString(36).slice(2, 8);
store[code] = longUrl;
res.status(201).json({ code, shortUrl: `/${code}` });
});
// Redirect to the original URL
app.get('/:code', (req, res) => {
const longUrl = store[req.params.code];
if (!longUrl) return res.status(404).json({ error: 'Not found' });
res.redirect(longUrl);
});
app.listen(3000);What is the bug in this REST API design?