@@ -2,61 +2,68 @@ const Store_Url = require('../models/url.models.js');
22const shortid = require ( 'shortid' ) ;
33const validator = require ( 'validator' ) ;
44
5- async function createShortUrl ( req , res ) {
5+ module . exports . createShortUrl = async ( req , res ) => {
66
77 let { url, alias } = req . body ;
8+
89 if ( ! url ) return res . status ( 404 ) . json ( { message : "Please provide a url" } ) ;
10+
911 url = url . trim ( ) ;
12+
1013 if ( ! validator . isURL ( url ) ) {
1114 return res . status ( 400 ) . json ( { message : "Invalid URL..." } ) ;
1215 }
1316
1417 alias = alias ? alias . trim ( ) : null ;
1518
16- if ( alias ) {
17- let urlExists = await Store_Url . findOne ( { shortId : alias } ) ;
19+ try {
20+ if ( alias ) {
21+ let urlExists = await Store_Url . findOne ( { shortId : alias } ) ;
1822
19- if ( urlExists ) {
20- return res . status ( 400 ) . json ( { message : "Alias is already taken" } ) ;
21- } else {
22- let targeturl = / ^ h t t p s ? : \/ \/ / i. test ( url ) ? url : `https://${ url } ` ;
23+ if ( urlExists ) {
24+ return res . status ( 400 ) . json ( { message : "Alias is already taken" } ) ;
25+ } else {
26+ let targeturl = / ^ h t t p s ? : \/ \/ / i. test ( url ) ? url : `https://${ url } ` ;
2327
24- await Store_Url . create ( {
25- shortId : alias ,
26- redirectUrl : targeturl ,
27- visitHistory : [ ] ,
28- owner : req . user . id ,
29- name : getDomainWithoutTLD ( targeturl )
30- } ) ;
28+ await Store_Url . create ( {
29+ shortId : alias ,
30+ redirectUrl : targeturl ,
31+ visitHistory : [ ] ,
32+ owner : req . user . id ,
33+ name : getDomainWithoutTLD ( targeturl )
34+ } ) ;
3135
32- return res . status ( 200 ) . json ( { new : alias } ) ;
36+ return res . status ( 200 ) . json ( { new : alias } ) ;
37+ }
3338 }
34- }
3539
36- let rand = shortid . generate ( ) . slice ( 0 , 7 ) ;
40+ let rand = shortid . generate ( ) . slice ( 0 , 7 ) ;
3741
38- let urlExists = await Store_Url . findOne ( { shortId : rand } ) ;
39- while ( urlExists ) {
40- rand = shortid . generate ( ) . slice ( 0 , 7 ) ;
41- urlExists = await Store_Url . findOne ( { shortId : rand } ) ;
42- }
43- let targeturl = / ^ h t t p s ? : \/ \/ / i. test ( url ) ? url : `https://${ url } ` ;
42+ let urlExists = await Store_Url . findOne ( { shortId : rand } ) ;
43+ while ( urlExists ) {
44+ rand = shortid . generate ( ) . slice ( 0 , 7 ) ;
45+ urlExists = await Store_Url . findOne ( { shortId : rand } ) ;
46+ }
47+ let targeturl = / ^ h t t p s ? : \/ \/ / i. test ( url ) ? url : `https://${ url } ` ;
48+
49+ await Store_Url . create ( {
50+ shortId : rand ,
51+ redirectUrl : targeturl ,
52+ visitHistory : [ ] ,
53+ owner : req . user . id ,
54+ name : getDomainWithoutTLD ( targeturl )
55+ } ) ;
4456
45- await Store_Url . create ( {
46- shortId : rand ,
47- redirectUrl : targeturl ,
48- visitHistory : [ ] ,
49- owner : req . user . id ,
50- name : getDomainWithoutTLD ( targeturl )
51- } ) ;
57+ res . status ( 200 ) . json ( { new : rand } ) ;
5258
53- res . status ( 200 ) . json ( { new : rand } ) ;
59+ } catch ( error ) {
60+ console . error ( error ) ;
61+ res . status ( 500 ) . json ( { error : "Something went wrong please try again later." } ) ;
62+ }
5463}
5564
5665function getDomainWithoutTLD ( url ) {
5766 const hostname = new URL ( url ) . hostname . replace ( "www." , "" ) ;
5867 const parts = hostname . split ( "." ) ;
5968 return parts . length > 2 ? parts . slice ( 0 , 2 ) . join ( "." ) : parts [ 0 ] ;
60- }
61-
62- module . exports = createShortUrl ;
69+ }
0 commit comments