diff --git a/index.js b/index.js index 8e05642..bd63da1 100644 --- a/index.js +++ b/index.js @@ -94,6 +94,7 @@ app.post('/shorten', (req, res) => { // Shorten URL if (req.body.passcode !== passcode) { return res.status(403).json({ error: 'Invalid passcode' }); } + // Generate a 8 character long string, only if { shortUrl } doesnt exist in body const shortUrl = req.body.shortUrl || Math.random().toString(36).substr(2, 8); // Check if shortUrl is already in use @@ -116,6 +117,7 @@ app.post('/shorten', (req, res) => { // Shorten URL }); }); + app.patch('/:shortUrl', (req, res) => { if (req.body.passcode !== passcode) { return res.status(403).json({ error: 'Invalid passcode' }); @@ -123,6 +125,7 @@ app.patch('/:shortUrl', (req, res) => { if (!req.body.url) { return res.status(400).json({ error: 'Please provide a URL' }); } + const shortUrl = req.params.shortUrl; db.get('SELECT * FROM urls WHERE shortUrl = ?', [shortUrl], (err, row) => { if (err) { console.error(err);