From 4e7c327966e039d7fdf32e19bb2d23c4e9abb4bf Mon Sep 17 00:00:00 2001 From: unurled Date: Thu, 1 Sep 2022 09:31:07 +0200 Subject: [PATCH] update mongoose and change the validation url process --- .gitignore | 4 +++- package.json | 6 +++--- src/service/redirect.js | 2 ++ src/service/url.js | 29 ++++++++++++++++++++++++++--- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index d7053f4..6e7113e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ .env node_modules package-lock.json -start.sh \ No newline at end of file +start.sh +yarn.lock +yarn-error.log \ No newline at end of file diff --git a/package.json b/package.json index 9ce70ad..ad7cf42 100644 --- a/package.json +++ b/package.json @@ -12,11 +12,11 @@ "dependencies": { "dotenv": "^16.0.0", "express": "^4.17.1", - "mongoose": "^6.0.5", + "mongoose": "^6.5.4", + "node-uri": "^1.1.1", "prom-client": "^14.0.1", "short-id": "^0.1.0-1", - "shortid": "^2.2.16", - "valid-url": "^1.0.9" + "shortid": "^2.2.16" }, "devDependencies": { "autoprefixer": "^10.4.0", diff --git a/src/service/redirect.js b/src/service/redirect.js index 8d7c4cc..64645d1 100644 --- a/src/service/redirect.js +++ b/src/service/redirect.js @@ -17,8 +17,10 @@ router.get('/:code', async (req, res) => { }) if (url) { // when valid we perform a redirect + console.log("Redirecting to: " + url.longUrl); return res.redirect(url.longUrl) } else { + console.log("Redirecting to Home.") return res.sendFile(path.join(__dirname + `../../../public/index.html`)) // else return a not found 404 status //return res.status(404).json('No URL Found') diff --git a/src/service/url.js b/src/service/url.js index fefff96..40ec8f5 100644 --- a/src/service/url.js +++ b/src/service/url.js @@ -1,7 +1,7 @@ require('dotenv').config() // packages needed in this file const express = require('express') -const validUrl = require('valid-url') +const uri = require('node-uri') const shortid = require('shortid') // creating express route handler @@ -16,13 +16,34 @@ const Url = require('../models/url') // The API base Url endpoint const baseUrl = process.env.BASEURL +function validUrl(url) { + let valid + try { + uri.checkHttpURL(url) + valid = true; + } catch { + valid = false; + } + if (!valid) { + try { + uri.checkHttpsURL(url) + valid = true; + } catch { + valid = false; + } + } + return valid; +} + router.post('/shorten', async (req, res) => { const { longUrl } = req.body // destructure the longUrl from req.body.longUrl // check base url if valid using the validUrl.isUri method - if (!validUrl.isUri(baseUrl)) { + + if (!validUrl(baseUrl)) { + console.log("Invalid url " + baseUrl) return res.status(401).json('Invalid base URL') } @@ -30,7 +51,7 @@ router.post('/shorten', async (req, res) => { const urlCode = shortid.generate() // check long url if valid using the validUrl.isUri method - if (validUrl.isUri(longUrl)) { + if (validUrl(longUrl)) { try { /* The findOne() provides a match to only the subset of the documents in the collection that match the query. In this case, before creating the short URL, @@ -42,6 +63,7 @@ router.post('/shorten', async (req, res) => { // url exist and return the response if (url) { + console.log("Return " + url) res.json(url) } else { // join the generated short code the the base url @@ -55,6 +77,7 @@ router.post('/shorten', async (req, res) => { date: new Date() }) await url.save() + console.log("Return " + url) res.json(url) } }