Finished URL Shortener -- Code Review

I didn’t focus too much on the front-end stuff, but I think I think it’s enough to be intuitive :slight_smile:

source: https://murmuring-reef-69157.herokuapp.com/
code: https://github.com/Neotriz/url-shortener

Can someone take a look at my app.js code? I am not sure if I can clean up the code better.

Any inputs will be appreciated!

Hey there, I had look at your code I don’t get why you are making two collections calls to the urls then lookups collection.

If I was you I would do something like make it easier with your mongoose schema like

{
  _id: ...
  longurl: {
    type: String,
    unique: true
  },
  shorturl: {
    type: String,
    unique: true
  }
}

That way your db will throw an error when you insert a shorturl or longurl that is already in the db. Also I think you could do your url validation in mongoose by creating an your own schema type.
http://mongoosejs.com/docs/customschematypes.html

another thing is either to hide your connection string. i.e require the string in and put it in .gitignore because from my home I can get into your heroku db

$ mongo mongodb://yourstringinyourgitfile
> db.urls.drop() // uh oh!
1 Like

Here is a nice website helps you generate gitignore files depending your project type.
I use it for my ASP.Net projects.


Create useful .gitignore files for your project

1 Like

Thanks for the taking your time looking at my code!
Yeah I’ve definetaly over-thought the logic behind this. I think I was trying to make it like a relational-database, which is probably why I made two collections instead.

Doh! I forgot about hiding the mongodb connection. I knew I was missing something…

Thanks again!

1 Like

Yep! I forgot about to put .gitignore for my db connection :slight_smile: Thanks !