Monday, March 10, 2014

No Update

Work has been keeping me way too busy the last few days, but I'm still keeping up my Github streak.  I'm currently (npm install is running now) trying to set up a MEAN stack with ExtJS 4 included.  Yeah, it's crazy, but I'm just goofing around.

It's interesting to see how Ext uses controllers.  They all appear to be bound to the viewport rather than within the scope of an element like in Angular.  For my commit tonight, I'm going to see if I can figure out a way to bind the Ext controllers the scope of an element.  Hopefully, after this sprint, I'll get out of the mire and back into the cool stuff.  Look at the last blog - I'm deploying to Heroku in minutes!  That's way more fun that what's happening at work.

An Aside

This really has nothing to do with my pursuit of MEAN-ness, but I'm going to include it.  Spaghetti code, hacking things together, ignoring conventions, and using design patterns only in conversation are so freaking bad!  There's a reason people identify patterns and anti-patterns.  Just because you're under pressure to make a deadline does not make it OK to avoid the collective wisdom of thousands of people who are more experienced and smarter than you.  In fact, it's the opposite.  A quick "win" today will inevitably lead to heartache later on.  Don't be short-sighted.

This message brought to you by the poor asshole who has to maintain, refactor, fix, and build on your mess.

Friday, March 7, 2014

Notes: Deploying MEAN Boilerplate to Heroku

Notes: Deploying MEAN Boilerplate to Heroku

Create the Heroku App

heroku create

Push to Heroku

git push heroku master

View the App

heroku open

WTF? Application Error

Mongo is not available.

Setup Mongo

MongoHQ offers free hosting for MongoDBs. Heroku has a free addon for MongoHQ.

Add MongoHQ

heroku addons:add mongohq
Heroku wants my credit card before allowing the addon. Um, no.

Use MongoHQ with Heroku without a Credit Card

The Heroku addon basically just sets up your HerokuHQ stuff for you. If you want to skip the credit card, you can set it up yourself.

Setup MongoHQ

Create a MongoHQ account. Choose the free sandbox version.
Once you're set up, find the url to your sweet new db. It looks something like: mongodb://:@troup.mongohq.com:10027/reergymerej
Create a user for the db through the web interface. I'm sure you can do it from terminal, but you're already here.
Now go to your terminal. :) Verify that you can connect.
mongo troup.mongohq.com:10027/reergymerej -u <user> -p<password>
or
mongo troup.mongohq.com:10027/reergymerej db.auth('dude', 'dude');

Connect Your App

See if you can connect to your MongoHQ db from your local MEAN app.
Where do you change the db settings?
/config/env/development.js
Change the db from localhost to the url you got above (the one with the username/password in it).
db: 'mongodb://dude:dude@troup.mongohq.com:10027/reergymerej',

Welcome to Error Town

/home/grizzle/code/the-runs/node_modules/mongodb/lib/mongodb/connection/base.js:242
    throw message;
I know the authentication works since I can connect through the terminal directly. I'm too tired to figure this out tonight.

Try, Try Again

The problem was with my copy/paste. In /config/env/development.js, I specified
db: 'mongodb://dude:dude@troup.mongohq.com:10027/',
instead of
db: 'mongodb://dude:dude@troup.mongohq.com:10027/reergymerej',
Dur.
I tested locally pointing at the remote db and everything's working. I confirmed through the UI and terminal.
mongo troup.mongohq.com:10027/reergymerej db.auth('dude', 'dude'); db.runs.find().pretty();
Now, let's deploy this sucker. Change db in production
/config/env/production.js

Commit and Push

git add . git commit -m 'change db config for production' git push heroku master
This pushes from our local repo to Heroku, not from our Github repo. Changes must be commited to the local git repo before they can be pushed to Heroku.
Around this point, my notes degrade pretty quickly. I changed the production db to the MongoHQ db and kept the dev db pointing at localhost. Heroku deployed alright, but the app wouldn't respond in the browser. heroku logs mentioned something about failing to connect to localhost, so I figured the app still thought it was in dev mode. For expediency, I changed the dev db to the remote also, deployed again, and everything started working.
Along the way, I noticed there's a MONGOHQ_URL in /config/env/all.js. We should probably use that, but lunch breaks are only so long. I chewed up a bit of my time researching how to keep from exposing credentials in Github.

Keep Production Credentials off Github

We use git to deploy to Heroku. We also use git to save our code on Github. How can we keep our production credentials in git, available to push to Heroku, but not available to Github?
I haven't quite figured it out and that's OK since my username &password are both 'dude' and no one's going to bring down my test db rocking one dyno. It's a good question, though. Here are a couple references I found:

tl;dr

  • Heroku doesn't provide MongoDB.
  • Create the db in HerokuHQ.
  • Configure MEAN environments to use HerokuHQ.
  • Don't push username/password to Github.

References

Wednesday, March 5, 2014

How to get Bootstrap with Bower

This is a quick post to explain how you can use Bower with your cool new MEAN stack. For this example, we'll install Bootstrap.

Install the Package(s)

bower install bootstrap

OK, all done. No, not really. All we did was download the files. They're not automatically added to our html somehow.

Where does Bower put files?

Out of the box, the MEAN stack puts Bower's files in /public/lib. This is a configuration setting in .bowerrc.

"directory": "public/lib",

How to Use the New Files

The lib directory is public, so we can access them directly. For simplicity, you can add it to /app/views/includes/head.html.

<script src="/lib/bootstrap/dist/js/bootstrap.min.js"></script>

What about dependencies?

Dependencies will be installed automatically by Bower. That's one of the main reasons to use a package manager. Boostrap has a dependency on jQuery, so it has been installed as well.

How do you know what dependencies to use?

When you install a package, the dependencies are listed.

bower install bootstrap#3.0.3 bower install jquery#2.1.0

bootstrap#3.0.3 public/lib/bootstrap
└── jquery#2.1.0

You can view all your current packages and their dependencies with

bower list

You can view the dependencies in the package's bower.json file. /public/lib/bootstrap/bower.json shows that bootstrap needs jQuery.

"dependencies": {
    "jquery": ">= 1.9.0"
}

Include the dependencies the same way as the main package. <script src="/lib/jquery/dist/js/jquery.min.js"></script>