Hey, it’s me again. Let’s take a walk in the code of this soon to be such a magnificent Node.js application.

Node.js API magic

We’ll start again with the API magic. Hopefully you’ll also better understand the choices of the technologies by seeing what happens in the backyard.

We’ll start the with the Node.js + Express REST API.


/**
* Generate new client key and add it to database
*
* Builds key using client IP and shop_key
* shop_key must be sent as GET param
*/
app.get('/client/new/:sid', function (req, res) {
    var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
    var db = req.db;
    var cClients = db.get('clients');

    var shop = req.params.sid || '123456789abcdefghi';
    var key = md5(ip + ' ' + shop);

    cClients.insert({ client_key : key, ip : ip, shop : shop });

    res.end(key);
});

Can you now see how simple that is? What happens in this code snippet is the following:

  • First we tell Express that we expect a GET request on the /client/new/$id route.
  • When this route is called, we get the IP of the user and use it along with the shop_key to generate a client_key which we insert in the database.
  • At the end we return this key.

And BAM!

Node.js and Express allow us to write a working REST API so fast and simple that here at AGVision people don’t feel they work. Yeah, I know, it’s awesome to be us.

 

Let’s see how the JavaScript API looks.

Tracker = function (apikey) { // var tracker = new Tracker('123456789abcdefghi')
    if (this._checkReferrer()) {
        this._set('fromReferrer', 1, true);
    } else {
        this._set('fromReferrer', 0, true);
    }

    if (typeof this._get('customer_id') === 'undefined') {
        qwest.get('/client/new/'+apikey)
            .then( function (x, r) {
               this._set('customer_id', r);
            });
        }

    this.pathname = window.location.pathname;
    this.search   = window.location.search;
    this.apikey   = apikey;
}

Even though this is just a sample, as you can see this is not complicated at all. That’s because we like to Keep It Simple Stupid. Although this is not exactly what the API looks; the calls to the REST API are not made from JavaScript, but they are passed to PHP and made from there for security reasons.

 

So far so good. I’m about to give you a sneak peak of the dashboard code. Prepare for the worst nightmare in the programming history:

/**
 * Show current user billing settings and history
 * 
 * @return \Illuminate\Http\Response
 */
public function billing ()
{
    return view('user.billing');
}

/**
 * Update current user billing settings
 */
public function updateBilling(Request $req)
{
    $req->user()->update($req->all());

    Session::flash('success', 'You billing has been successfully updated!');

    return back();
}

Yeah you guessed it, i was joking again! I’m such a funny fellow, right? xD

So, these two methods you see right there are completely functional and allow you to display current user billing settings AND it updates them when you submit the form. Of course the view has a little more code, but that’s just visuals, it’s supposed to be like that. This is the backend part of the admin and it’s so simple to put in place! OMG i’m hyperventilating.

 

With this I just wanted to show you how tidy the code can be using the right tools for the right project and to further justify our choice of tools. Hope to see you soon guys with more of my awesome programming skills!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>