URL Shortener Review & Questions

I’ve just finished my URL Shortener!
First I have to say I followed a tutorial in order to get this done.
http://lefkowitz.me/thoughts/2016/05/05/men-stack-building-a-url-shortener-with-mongodb-express-and-node-js/
(The link died just a few hours ago, right after I finished…holy ****)

live app : https://st3ps-urlshortener-api.herokuapp.com/
github repo : https://github.com/St3ps/url-shortener-api/tree/master

If you’d like to review you’re most welcome.

A question:
I’ve made a branch specific for Heroku. In github you don’t need to see the config file where i have the username and password credentials. But Heroku in order to run the live app will need that file.
So that’s why i made a Heroku branch. I was just following the tutorial and as some of you might’ve thought: Yes, the branch is on github all the same with the username and password to my mongodb mlab database, just not in the master branch. So I have to ask how could I have it done in such a way that i could’ve kept my credentials private?

1 Like

Hey, short_url still starts with localhost.

To keep your credentials private, you could use environment variables. You can set those (somewhere) on Heroku and then use the dotenv npm package for developing.

2 Likes

I did it like this:

Created a file credentials.js with content:

'use strict';

module.exports = {
  DB_URL: `mongodb://myUsername:myPassword@blablabla.mlab.com:12345/myDatabase`
}

and added it to .gitignore.

Then in the file where I need this url I required:

...
const url = process.env.DB_URL || require('./credentials').DB_URL
...

(this way Node looks for env value DB_URL and if there is none (on my local machine) it looks for the file credentials.js)

Then added an environmental variable to Heroku:

heroku config:set DB_URL='mongodb://myUsername:myPassword@blablabla.mlab.com:12345/myDatabase'

Seems private to me :slight_smile:


Here you can check my API:
https://fcc-vj-url.herokuapp.com/
Source Code:
https://github.com/jenovs/fcc-url-shortener-ms

4 Likes

Hi guys, thank you for your hints and suggestions.

I’m at the moment looking into the app, I seem to have broken it :confused:

Ok, got it working!

@BenGitter
As sugested I used enviromnent variables, and while dotenv seemed attractive for something of this small scale i just limited to simply add the var locally with an export command in Cloud 9 and in Heroku i just added the enviromnent var in the dashboard and that was it – but i’ll definitely keep it in mind for the future, i had actually seen dotenv before in github repos and sort of glanced through it in confusion. At the time:i was more concerned with looking for a specific code :stuck_out_tongue:

@jenovs
did you use Heroku CLI to add an enviromental variable to heroku? If so, did you install it on Cloud 9?

Again, thanks everyone

1 Like

Yes, I used Heroku CLI.
I don’t use Cloud9, I develop locally.

1 Like

Cool. I always seem to over engineer. I used Redis instead.

https://fathomless-woodland-80063.herokuapp.com/

I just noticed in your app’s page, you have a text problem (I’m using Firefox, and the 2nd code paragraph seems to have overlapping text? It’s very odd), but i like your approach by example way of introducing the app.

@jboxman
Looks pretty good. So why did you use Redis?

Because it felt right @St3ps. I didn’t want to pay the setup cost for MongoDB. I’d want to learn Mongoose along with it, which I ultimately will at some point. Plus Redis seemed ideal for this application. In my implementation, I don’t care about duplicate entries, so it’s basically just a running log of every URL/key pair ever added. Anytime a valid URL is added, it also is recorded in a top N list. Redis is un-opinionated in how you use it and it is packed with operators for each data structure.

Highly recommend exploring it!

1 Like

Thank you. Corrected.

As the landing page wasn’t required I didn’t spent too much time to test the styling :wink:

1 Like

Glad my tutorial helped you out. I have learned a lot since that time and was actually currently working on a revision to that tutorial to clean things up a bit.

As you pointed out, my host seems to have disappeared off the face of the planet as of yesterday.

Working on rebooting my site now. And just last week I was thinking about making backups…

1 Like

And here I was thinking you had forgotten to pay the bills! :stuck_out_tongue:

Your tutorial is a great help, I’m looking forward to see your revision

Good to hear. Found a backup - back online now. Guess that’s what happens when your host costs $3/yr.

hello everybody i’m working right now on the url shortened api but i didn’t now what is our goal from this api why any one could develope such an api