# Url Shortener Code Review

**URL:** <https://forum.freecodecamp.org/t/url-shortener-code-review/56214>\
**Category:** I Built This\
**Created:** [November 17, 2016, 3:14am UTC](https://forum.freecodecamp.org/t/url-shortener-code-review/56214 "2016-11-17T03:14:03Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![twmilli](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/twmilli/32/3187_2.png) [@twmilli](https://forum.freecodecamp.org/u/twmilli)\
**Post date:** [November 17, 2016, 3:14am UTC](https://forum.freecodecamp.org/t/url-shortener-code-review/56214/1 "2016-11-17T03:14:03Z")

</div>

Hey guys, i just finished the first somewhat substantial backend project and would love to get some tips on how I did/how to improve my code going forward. If anyone has some free node/express videos/articles that they could recommend as well that would be awesome, especially for better understanding middleware.

repo: [https://github.com/twmilli/url\_shortener](https://github.com/twmilli/url_shortener)  
app: [https://secure-crag-25604.herokuapp.com/](https://secure-crag-25604.herokuapp.com/)

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![ZackWard](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zackward/32/159484_2.png) [@ZackWard](https://forum.freecodecamp.org/u/ZackWard)\
**Post date:** [November 17, 2016, 3:42am UTC](https://forum.freecodecamp.org/t/url-shortener-code-review/56214/2 "2016-11-17T03:42:53Z")

</div>

I’m getting an application error when I try to use the app. ☹

---

<div class="post-metadata">

**Author:** ![JacksonBates](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jacksonbates/32/123402_2.png) [@JacksonBates](https://forum.freecodecamp.org/u/JacksonBates)\
**Post date:** [November 17, 2016, 4:20am UTC](https://forum.freecodecamp.org/t/url-shortener-code-review/56214/3 "2016-11-17T04:20:05Z")

</div>

It works for me - what were you trying to do when it failed?

---

<div class="post-metadata">

**Author:** ![ZackWard](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zackward/32/159484_2.png) [@ZackWard](https://forum.freecodecamp.org/u/ZackWard)\
**Post date:** [November 17, 2016, 4:21am UTC](https://forum.freecodecamp.org/t/url-shortener-code-review/56214/4 "2016-11-17T04:21:51Z")

</div>

I had just attempted to create a new short link. It seems to be working now though.

---

<div class="post-metadata">

**Author:** ![JacksonBates](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jacksonbates/32/123402_2.png) [@JacksonBates](https://forum.freecodecamp.org/u/JacksonBates)\
**Post date:** [November 17, 2016, 4:27am UTC](https://forum.freecodecamp.org/t/url-shortener-code-review/56214/5 "2016-11-17T04:27:50Z")

</div>

You’ve structured yours the way I did mine the first time - I actually redid mine again from scratch after seeking feedback on Gitter…

Here was the exchange regarding my old one on gitter - I scrubbed their username so they don’t get pinged by me posting this 🙂 :

> **username scrubbed** May 22 21:29  
> @JacksonBates : It indeed is horrible You gotta modularize it.  
> The very first problem I see here is, every time your server receives a request, you server is going to open a new connection to the db port. Connecting to db is one of the most time consuming activities you can do.  
> What you need to do is cache the connection and reuse it afterward, instead of opening it over and over again.

> Jackson Bates @JacksonBates May 22 21:38  
> so would I do that by opening the db connection outside of the various 'app.get’s?  
> I tried modularizing before, but I have trouble passing request and response objects around like that…I didn’t really feel like I learned modules from the Learnyounode lessons - are there other resources for learning modules you’d recommend?

> **username scrubbed** May 22 21:48  
> @JacksonBates

1. Separate your db logic from routing logic.  
I’ll share some review comments in some time. I am at work right now.  
You can take a look at FCC code base on github to see how they’re achieving modularity.

> Jackson Bates @JacksonBates May 22 21:51  
> Thanks, I appreciate the pointers. I think I have enough to go on for now - but if there are glaring issues you haven’t mentioned yet and you have time to point them out later that would be great. You’ve been really helpful so far - cheers

---

<div class="post-metadata">

**Author:** ![twmilli](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/twmilli/32/3187_2.png) [@twmilli](https://forum.freecodecamp.org/u/twmilli)\
**Post date:** [November 17, 2016, 5:12pm UTC](https://forum.freecodecamp.org/t/url-shortener-code-review/56214/6 "2016-11-17T17:12:57Z")

</div>

@JacksonBates thank you so much, this is exactly what I was looking for! I went back and checked out how you structured yours and did a major refactor and I feel so much better about my code now. Even though it doesn’t make too much difference on small apps, I know this will be huge once I get started on the full stack projects!

---

<div class="post-metadata">

**Author:** ![twmilli](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/twmilli/32/3187_2.png) [@twmilli](https://forum.freecodecamp.org/u/twmilli)\
**Post date:** [November 17, 2016, 5:22pm UTC](https://forum.freecodecamp.org/t/url-shortener-code-review/56214/7 "2016-11-17T17:22:55Z")

</div>

As a followup, are any of you guys using local virtual environments for backend development over c9? I’d like to switch over but I tried once and found the process confusing and couldn’t quite get everything to work. If anyone has any tutorial recommendations/general advice, I would love to hear them!

---

<div class="post-metadata">

**Author:** ![BenGitter](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bengitter/32/12192_2.png) [@BenGitter](https://forum.freecodecamp.org/u/BenGitter)\
**Post date:** [November 17, 2016, 6:59pm UTC](https://forum.freecodecamp.org/t/url-shortener-code-review/56214/8 "2016-11-17T18:59:43Z")

</div>

I totally agree with the rest, that your code is nicely structured.

I haven’t used MongoDB a lot, but does this code only create a document if there isn’t already one for the specific url?

```auto
db.collection("urls")
        .update(
            {key: obj.key}, obj, {upsert: true});

```

Setting up a local environment can be quite easy (but it can also be quite difficult). What have you done already? Are you using Windows?

---

<div class="post-metadata">

**Author:** ![Bigghead](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bigghead/32/28557_2.png) [@Bigghead](https://forum.freecodecamp.org/u/Bigghead)\
**Post date:** [November 17, 2016, 9:27pm UTC](https://forum.freecodecamp.org/t/url-shortener-code-review/56214/9 "2016-11-17T21:27:03Z")

</div>

‘What you need to do is cache the connection and reuse it afterward, instead of opening it over and over again’

I don’t get what they mean by this. When I did mine, I just used mongoose to connect to a db (locally) at the top of the code, and write all the routes underneath. Is that bad practice?

---

<div class="post-metadata">

**Author:** ![BenGitter](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bengitter/32/12192_2.png) [@BenGitter](https://forum.freecodecamp.org/u/BenGitter)\
**Post date:** [November 17, 2016, 9:34pm UTC](https://forum.freecodecamp.org/t/url-shortener-code-review/56214/10 "2016-11-17T21:34:48Z")

</div>

> [@Bigghead](#):
>
> ‘What you need to do is cache the connection and reuse it afterward, instead of opening it over and over again’

Make sure that you only once open the database connection (and let it remain open). So that you don’t have to make a connection to the database every time you handle a request.

I suppose you are doing it the correct way. As long as you are only making a connection to the database once.

---

<div class="post-metadata">

**Author:** ![JacksonBates](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jacksonbates/32/123402_2.png) [@JacksonBates](https://forum.freecodecamp.org/u/JacksonBates)\
**Post date:** [November 17, 2016, 9:47pm UTC](https://forum.freecodecamp.org/t/url-shortener-code-review/56214/11 "2016-11-17T21:47:58Z")

</div>

Mongoose handles the db connection that way naturally as I understand it. I’ve only used Mongoose once, and it did make things easier in that regard.

---

<div class="post-metadata">

**Author:** ![AdventureBear](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/adventurebear/32/74786_2.png) [@AdventureBear](https://forum.freecodecamp.org/u/AdventureBear)\
**Post date:** [November 17, 2016, 9:58pm UTC](https://forum.freecodecamp.org/t/url-shortener-code-review/56214/12 "2016-11-17T21:58:24Z")

</div>

This is a great thread .

---

<div class="post-metadata">

**Author:** ![yhabib](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/yhabib/32/164148_2.png) [@yhabib](https://forum.freecodecamp.org/u/yhabib)\
**Post date:** [December 4, 2016, 1:58pm UTC](https://forum.freecodecamp.org/t/url-shortener-code-review/56214/13 "2016-12-04T13:58:52Z")

</div>

@JacksonBates I took a look to your code, in particular the way you work with the db. Don’t you close it at any time right?? I read that Mongoose takes care of it, but I prefer to do it right now without it.

Thanks

---

<div class="post-metadata">

**Author:** ![JacksonBates](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jacksonbates/32/123402_2.png) [@JacksonBates](https://forum.freecodecamp.org/u/JacksonBates)\
**Post date:** [December 4, 2016, 10:57pm UTC](https://forum.freecodecamp.org/t/url-shortener-code-review/56214/14 "2016-12-04T22:57:00Z")

</div>

There’s no need to close the connection with either mongo or mongoose in this use case.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:01pm UTC](https://forum.freecodecamp.org/t/url-shortener-code-review/56214/15 "2021-06-01T18:01:57Z")

</div>

Closed on June 1, 2021, 6:01pm UTC


