const express = require('express'); const urls = express.Router(); const shortid = require('shortid'); const Url = require('../models/Url'); const utils = require('../utils/utils'); urls.post('/short', async (req, res) => { const { origUrl } = req.body; const base = process.env.BASE; const urlId = shortid.generate(); if (utils.validateUrl(origUrl)) { try { let url = await Url.findOne({ origUrl }); if (url) { res.json(url); } else { const shortUrl = `${base}/${urlId}`; url = new Url({ origUrl, shortUrl, urlId, date: new Date().toISOString(), }); await url.save(); res.json(url); } } catch (err) { console.log(err); res.status(500).json('Server Error'); } } else { res.status(400).json('Invalid Provided Url'); } }); module.exports = urls;