I just spent a few hours diving into node seriously for the first time. It is very cool, very slick, and very fast … but the libraries have lackluster documentation, in part because the devs iterate so damn fast. You’ll see my resulting code below, it’s a scaffold for something I need at work.

Enough babbling Here’s the code:

//server.js
var http = require('http'); //http lib used to build the 'client'  
var express = require('express'); //express helps us build the 'server' a bit faster

var app = express.createServer(); //used for serving up the main template and the js model 

//app.get('/models/data.js', function(req, res){ res.sendfile(__dirname + '/models/data.js') }) //serve up the model when requested specifically
app.use(express.staticProvider(__dirname + '/models')); //using static provide middleware to serve whole model directory

app.get('/panic', function(req, res){ //perform all this logic when /panic is requested

    var options = { host: 'localhost', port: 80, path: '/data.js' }; //for url connection

    var req = http.get(options, function(clientRes) { // http get, http.request is the more generic form
      var body = "";
      clientRes.on('data', function (chunk) { //on each chunk read in the data
        body+=chunk;
      });
      clientRes.on('end', function (){ //at the end, feed the downloaded model into the ejs template
        res.render('template.ejs', {
        locals:JSON.parse(body)
        });
      });
    });

});

//views/template.ejs
<% if (title) { %>  
    <h2><%= title %></h2>
<% } %>

//views/layout.ejs
<html>  
<head><title>node test</title></head>  
<body>  
<%- body %>  
</body>  
<html>

//models/data.js
{ "title": "My Site" }

Simple right? Well in order for me to understand all this I had to learn about, express, middleware, EJS templates, partials, clientResponse, etc….

Here’s a break down of the time sucks:

  1. So now I had my data, which I knew needed to be rendered with a template, but all I could find in the (ejs) documentation was about referencing models w/ the locals variable. I figured that couldn’t be all there was, ONE WAY to add data to a template! After 20 mins searching, the painful answer was yes … so you have to get remote data yourself and shoe horn it into locals… which brings me to my next item. Mustache is my favorite compared to Jade or EJS

  2. Now I needed the equivalent of URLConnection in node to pull my data out of the cloud:

    • I spent 15 minutes trying to figure out if express had this functionality, nope, it only helps with serving content
    • I spent 15 minutes looking in node JS documentation and found nothing
    • I asked on the IRC channel and got an immediate answer, http.get (which can be confusing considering server.get is the opposite)
    • went back to the documentation (another 15 min) to see if I could find it now, still no … I mean it’s there, in pieces, with no context about the kinds of problems it solves.
  3. For at least 30 minutes JSON.parse was broken for me, or so I thought. nodes error messages are very poor so I thought the library was not being imported. I was installing, uninstalling, reading, trying random crap. Turned out I made the dumb mistake of having var body; instead of var body = “”; which was putting undefined into the string. In the browser this would have been immediately apparent .. maybe I just need to learn more about node exception handling and logging.

  4. Layouts make sense and are simple but I spent more time searching around the web on the assumption that they were more powerful or complex, nope… just a place to stick your html and head tags =(

  5. Just as I was thrown off by variations of get, I was also thrown by server reponse vs client response.. I have been network programming for a long time, but because of javascript namespace ambiguity it just didn’t dawn on me.

Like I said very cool, but for a collection of subjects I am very familiar with: javascript, networking, etc … man, this learning curve was unexpected. Hopefully you can all learn from my experiences and have an easier time. Anyway I learned many more lessons during and since, so feel free to dm me.