Here is my URL Shortener Microservice

Hi everyone!

Had a lot to learn for this one, but just managed to finish my URL Shortener:

https://url-shortener-88digital.herokuapp.com/

It works I think and it’s a first attempt, but I need to refactor the code.

Any feedback welcome, thanks! One thing that is bothering me right now, is that I’m using mLab and have the DB url and password in an .env file. I am pushing to Heroku from GitHub, so I need to find a way to gitignore that file for GitHub but push it to Heroku without ignoring that file. Any advice on that is much appreciated!

4 Likes

You can set environmental variables for Heroku and then reference the variable in your JS.

The command is: heroku config:set VARIABLENAME=variablevalue

I notice you set the whole DB_URL as an env variable in your code. I took a slightly different approach and only save the username:password portion of the url. That way I can reuse the same variable across all my mlab based apps. That might be bad practice (I dunno) but it’s easier than maintaining a list of separate usernames and passwords for my sandbox databases :slight_smile:

Edit: You can do it via the heroku settings through your settings on their website - but one line in the terminal is quicker :slight_smile:

Hey cool app, I was curious how you’re using the .env file. Is this something specific for Heroku?

I’ve read about using some kind of config file with express to set environment variables but never understood fully how to setup a project this way.

I would like to store things like db connection strings, api keys etc…

Hi thanks! The method Jackson, thanks btw :slight_smile: , is mentioning is something specific for Heroku.

If you use the dotenv module in your app https://www.npmjs.com/package/dotenv, you can make a .env file in the root folder and set variables there you can use in your app with process.env. I have the database URL and password in there for this app. The reason for me was to gitignore this file so I can push everything to Github without exposing this info.

For example this is my .env file:

DB_URL = mongodb://:@databaseurl
APP_URL = https://url-shortener-88digital.herokuapp.com/

in my code:

var url = process.env.DB_URL;

MongoClient.connect(url, function(err, db) {

if (err) {

  console.log('Unable to connect to the mongoDB server. Error:', err);

} else {

  console.log('Connection established to', url);

});

In production I would then set the environment variables in Heroku with heroku config:set

I should point out, just in case you or other readers aren’t aware, that you can also set environment variables on your local computer as well, which can then also be referenced in your JS with the same variable. That way you never actually need a .env file. I’m not sure which is best practice or if there are trade-offs picking one method over the other, but FWIW I only use system env variables on my computer and heroku, and never a .env file.

I can’t remember the terminal commands for setting environment variables for each system off the top of my head, but it should be trivial to Google :slight_smile: I did all my recent stuff in Windows and you can access the environment variables through the control panel settings or through the Powershell terminal. I just switched to Linux and haven’t used env variables there yet…

nice one. I was reading this about setting environment variables http://stackoverflow.com/questions/22312671/node-js-setting-environment-variables . I work mostly in Cloud9 myself

Hey Jeremy, this might be helpful if you are using Cloud9 as well: http://stackoverflow.com/questions/25927513/storing-securely-passwords-for-connection-to-db-in-opensource-projects

@chemok78 I was reading about this module, i’ll definitely be trying this out on my current project soon. Thanks for the tip!

I’m not using cloud9 but looks like a very cool service.

Actually, I hadn’t seen the dotenv module before. It looks like it’ll simplify my deployment/development process. Think I might start using that instead :slight_smile:

I’m currently working on this app. I got it functioning, at least to a point that I’m ready for a first deploy, but I’m using heroku for the first time. Did the whole thing in c9, using the .env module and all, but so far still haven’t gotten anything but an application error page.

My problem is that I’m using a free account and need 2 workers in my Procfile to a) run mongod and b) switch databases in the mongoshell. Some googling has all pointed me toward installing mlab, but does mlab not require you to run another dyno? Or am I going to have to pay to make this work? and will I have to rewrite my script for mlab from my regular ol’ mongodb setup?

Sorry if any of these questions are a little elementary, I’m very new to backend.

MLab is an external service, so you don’t need to run mongod as another service. Just point your database connection at the URL for your mLab db and you’re good to go :slight_smile:

Are you sure you are not gitignoring your .env file? Then you need to set your environment variables for Heroku for production manually. Here is how to do it from the command line:

In my case, I use the .env file in development. But then pushed to production in Heroku, you this .env file is not sent. The login to your mLab account should be the same in production

I am gitignoring my .env file. Set the variables. I had referenced this thread quite a bit in doing so, and am grateful to you and jacksonbates for it!

I was just reluctant with mlab since I wasn’t really sure how it worked. This project has been a long process for me. I ended up splicing it with a tutorial I found on coligo.io, then once I had a better understanding, I modified it to also meet the user stories of the FCC project. I finished just yesterday!

https://shortyourl.herokuapp.com/

Still debugging the error that is supposed to show when given an invalid url format. Worked on c9 but when deployed to heroku, instead of my error it displays an “Internal Server Error”.
Guess I need to read up on error handling a bit more :unamused: