Friday, August 22, 2014

What's so great about meanjs?

Yarr
(Note: this is a more technically-oriented article, and I plan to write another, more accessible version in the near future)
I'm sure you've heard the hype: "Node is taking over.  Batten down the hatches, me hearties."  And they're right.  With the new fullstack javascript frameworks that are coming out (namely meanjs and meanio), node, angular, and all their buddies are taking the development world by storm.  To implement a basic blogging service with user accounts, passport authentication, a complete json api and more is as simple as typing
yo meanjs
The way things are going with things like alchemyAPI, node robots and web sockets, us developers will be out of a job some day, which leads me to my point: meanjs is the greatest thing ever (so far).  So, what is meanjs, exactly? (I apologize in advance for all the mean puns)



  • What "mean" means
  • What mean means for me
    • I don't have to learn 4 (four)  flippin' different languages!  Back in the good old days, people needed to learn PHP and SQL for the backend and JavaScript and ActionScript on the frontend.  This process was further complicated when other tools and frameworks emerged, like Java Applets.
  • Wait, how does meanjs change that?
    • From the meanjs site: "MEAN.JS is a full-stack JavaScript solution that helps you build fast, robust and maintainble production web applications using MongoDB, Express, AngularJS, and Node.js."
    • "a full-stack JavaScript solution"
    • You only have to learn one programming language!  That is a very big deal.
  • So why should I use meanjs instead of rolling my own full-stack js framework?
    • One word: boilerplate
    • meanjs is distributed via a yeoman generator
      • This tool autogenerates content common to every fullstack js application, thereby infinitely expediting the development process
      • It even comes with bootstrap ❤ (a beautiful html and css boilerplate compilation)
  • yeoman generator
    • The meanjs yeoman generator generates static boilerplate code fine, but it also generates application-specific modules for you as well
      • The crud module generator automatically generates frontend and backend modules for segments of your application.  However, this is no simple ajax.  In fact, every crud module generated is turned into a REST endpoint, so your entire site interacts using public (or private, should you choose) api calls.
      • This means the api won't get neglected: the usefulness of the application is directly related to how good your api is
      • How to use the crud module generator:
        • yo meanjs:crud-module modulename
  • So, how much does this meanjs thing cost me?
    • Nothing, it's free and open source software (github page) thanks to the superheroic efforts of Amos Haviv :-)
  • Ok, I'm ready to learn this framework.  How many years will it take?
    • Don't make me laugh.  If you've got a good deal of determination, and the ability to reason through problems, you can be up and producing within 2 months.  The framework is, after all, only in its infancy
This framework is so exciting because anyone can pick it up and be superheroic while working with it.  I want to teach everyone that will listen how to code.  With these modern frameworks that make programming so accessible, I plan to share this knowledge with historically underrepresented demographics in computer science, and to level out the playing field for the future.  Frameworks like this will change the world.

Tuesday, August 19, 2014

How to use OAuth with MEANJS

Nodejs logo
Hello, this is a short tutorial on how to use OAuth with meanjs.  It can be followed using another framework, but there are some mean-specific tips in here.  You probably do need node, though.







  • To use oauth with meanjs, you should find a library that has already implemented the handshaking for you (googleapis and Twit)
  • A note about OAuth 1 vs 2:
    • OAuth support is fragmented
    • Lots of node libraries support both
    • Not all services support 1 or 2
  • Note: if you plan on using user-based authentication along with application authentication, it is much easier to use the existing passport integration that meanjs already has in place
    • This makes obtaining session tokens much easier
  • Most often, these libraries will provide easy-to-use interfaces (APIs) where keys are passed as a config object
    • demoTwit.png
    • All you need to do is put in your keys where they are needed
  • To get access tokens, most of the time you need to create a new app
  • These instructions are pretty straightforward usually
  • The callback url can be found in config/env/development.js under the object whose service name you are using
  • Note: if you use a non-standard port on your app, you must specify that port in the callback url (127.0.0.1:3000/callback/)
  • A note on twitter’s api app creator:
    • For some reason, twitter doesn’t like localhost to be in the callback url, so you need to specify localhost using 127.0.0.1 eg
    • 127.0.0.1:3000/auth/twitter/callback
  • This will give you one or two application-specific tokens or secrets
    • These application-specific keys are used to authenticate your app
    • If you need per-user authentication as well, read on
  • To fill in the per-user authentication tokens, you need to interface with passport and the existing meanjs framework for these things
    • These session keys can be obtained from the user object after authenticating eg
var providerData = req.user.providerData || req.user._doc.additionalProvidersData.twitter;

   var T = new Twit({
       consumer_key: config.twitter.clientID,
       consumer_secret: config.twitter.clientSecret,
       access_token: providerData.token,
       access_token_secret: providerData.tokenSecret
   });

  • The branch googleApiDemo of my fork of extra-mean has a demo of something like this, feel free to use it as an example
  • Any questions?  Leave a comment or email me at macsj200@gmail.com