Skip to content

Commit

Permalink
shortening the URL works now
Browse files Browse the repository at this point in the history
  • Loading branch information
usmanity committed Jul 25, 2021
1 parent bb38207 commit 405fb40
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions api/v1/create.js
@@ -1,6 +1,12 @@
const express = require('express');
const router = express.Router();

// router.use(express.urlencoded());
router.use(express.json());

const OUTPUT_LENGTH = require('./output_length.js').output_length();
const crypto = require('crypto');

router.route('/').get(getCreateHandler).post(postCreateHandler);

function getCreateHandler(req, res, next) {
Expand All @@ -13,14 +19,27 @@ function getCreateHandler(req, res, next) {
}

function postCreateHandler(req, res, next) {
// TODO: implement create short url
const { url } = req.body;
const shortUrl = createShortUrl(url);
res.send({ shortUrl });
if (url.includes('usman.xyz')) {
res.status(400).send({
error: 'URL cannot be from usman.xyz'
});
} else {
res.send({
shortUrl,
full_url: url,
more_info: `https://s.usman.xyz/${shortUrl}+`
});
}
};

function createShortUrl(url) {
return `/abc123`;
console.log(url);
const hash = crypto.createHash('sha1');
hash.update(url);
const shortUrl = hash.digest('hex');
return shortUrl.substring(0, OUTPUT_LENGTH);
}

module.exports = router;

0 comments on commit 405fb40

Please sign in to comment.