@@ -16,64 +16,64 @@ app.use(bodyParser.json());
1616app . use ( cors ( ) ) ;
1717
1818mongoose
19- . connect (
20- `mongodb+srv://${ process . env . MONGO_USER } :${ process . env . MONGO_PASSWORD } @cluster0-jwfcs.mongodb.net/${ process . env . MONGO_DB } ?retryWrites=true&w=majority` ,
21- {
22- useNewUrlParser : true ,
23- useUnifiedTopology : true ,
24- useCreateIndex : true ,
25- }
26- )
27- . then ( ( ) => {
28- console . log ( "MongoDb connected....." ) ;
29- } )
30- . catch ( ( err ) => {
31- console . log ( "Cannot connect to db due to " + err ) ;
32- } ) ;
19+ . connect (
20+ `mongodb+srv://${ process . env . MONGO_USER } :${ process . env . MONGO_PASSWORD } @cluster0-jwfcs.mongodb.net/${ process . env . MONGO_DB } ?retryWrites=true&w=majority` ,
21+ {
22+ useNewUrlParser : true ,
23+ useUnifiedTopology : true ,
24+ useCreateIndex : true ,
25+ }
26+ )
27+ . then ( ( ) => {
28+ console . log ( "MongoDb connected....." ) ;
29+ } )
30+ . catch ( ( err ) => {
31+ console . log ( "Cannot connect to db due to " + err ) ;
32+ } ) ;
3333
3434app . use ( "/api/urls" , url ) ;
3535app . use ( "/api/register" , users ) ;
3636app . use ( "/api/login" , auth ) ;
3737
3838app . get ( "/:code" , async ( req , res ) => {
39- try {
40- const url = await Url . findOne ( { code : req . params . code } ) ;
39+ try {
40+ const url = await Url . findOne ( { code : req . params . code } ) ;
4141
42- if ( url ) {
43- if (
44- url . lastDate &&
45- url . lastDate . toISOString ( ) < new Date ( ) . toISOString ( )
46- ) {
47- const user = await User . findById ( url . creator ) ;
42+ if ( url ) {
43+ if (
44+ url . lastDate &&
45+ url . lastDate . toISOString ( ) < new Date ( ) . toISOString ( )
46+ ) {
47+ const user = await User . findById ( url . creator ) ;
4848
49- for ( let i = 0 ; i < user . shortenedUrl . length ; i ++ ) {
50- if ( user . shortenedUrl [ i ] . toString ( ) === id ) {
51- user . shortenedUrl . splice ( i , 1 ) ;
52- break ;
53- }
54- }
55- await user . save ( ) ;
56- await url . deleteOne ( ) ;
57- return res
58- . status ( 404 )
59- . json ( { msg : "This Compress URL does not exist any more!" } ) ;
60- }
61- if ( ! url . status ) {
62- return res
63- . status ( 200 )
64- . json ( { msg : "This url is temporarily out of service!" } ) ;
65- }
49+ for ( let i = 0 ; i < user . shortenedUrl . length ; i ++ ) {
50+ if ( user . shortenedUrl [ i ] . toString ( ) === id ) {
51+ user . shortenedUrl . splice ( i , 1 ) ;
52+ break ;
53+ }
54+ }
55+ await user . save ( ) ;
56+ await url . deleteOne ( ) ;
57+ return res
58+ . status ( 404 )
59+ . json ( { msg : "This Compress URL does not exist any more!" } ) ;
60+ }
61+ if ( ! url . status ) {
62+ return res
63+ . status ( 200 )
64+ . json ( { msg : "This url is temporarily out of service!" } ) ;
65+ }
6666
67- url . clicks = url . clicks + 1 ;
68- url . save ( ) ;
69- return res . redirect ( url . longUrl ) ;
70- } else {
71- return res . status ( 404 ) . json ( { msg : "No Compressed URL Found" } ) ;
72- }
73- } catch ( err ) {
74- console . log ( err ) ;
75- res . status ( 500 ) . json ( { msg : "Internal Server Error" } ) ;
76- }
67+ url . clicks = url . clicks + 1 ;
68+ url . save ( ) ;
69+ return res . redirect ( url . longUrl ) ;
70+ } else {
71+ return res . status ( 404 ) . json ( { msg : "No Compressed URL Found" } ) ;
72+ }
73+ } catch ( err ) {
74+ console . log ( err ) ;
75+ res . status ( 500 ) . json ( { msg : "Internal Server Error" } ) ;
76+ }
7777} ) ;
7878
7979const port = process . env . PORT || 5000 ;
0 commit comments