Node.js: an app example using Yahoo geolocation service, and request library

This is a bad copy for personal use. The original series / tutorial here:

https://thenewcircle.com/s/post/1534/nodejs_tutorial_videos_geolocation_app?utm_source=javascriptweekly&utm_medium=email

1) Make sure node and npm are installed:

$node –version

$npm –version

2) Setup a local space for your project

$mkdir myapp; cd myapp;

$npm install express

Since we are not using -g to install this globally, this express installation is local to your project only. It will be installed under node_modules

$node_modules/express/bin/express

$npm install

This last command install any additional resouces listed in package.json

$npm start

If everything went fine, you should see your server running now at http://localhost:3000/

3) Install handlebars, and save any dependencies to your package.json file:

$npm install express3-handlebars

And, since we are using handlebars instead of jade, remove anything we don’t need, and bring what we do need:

$rm views/*.jade

$cp -R node_modules/express3-handlebars/examples/basic/views/* views/

4) The main application resides in app.js. Do the following modifications:

// Add this include
var exphbs = require(‘express3-handlebars’);

// app.set(‘view engine’, ‘jade’); Comment out jade

// Add in express3-handlebars.
app.engine(‘handlebars’, exphbs({defaultLayout: ‘main’}));
app.set(‘view engine’, ‘handlebars’);

//app.get(‘/’, routes.index); Get rid of routes
//app.get(‘/users’, user.list);

And start the app again, to see if everything still working:

$node app.js

To modify the home view, the file to edit is:

views/home.handlebars

5) Install bootstrap (optional)

$curl -C – -O http://getbootstrap.com/2.3.2/assets/bootstrap.zip

$unzip bootstrap.zip

$cp -R bootstrap/css/* public/stylesheets/

$rm -rf bootstrap bootstrap.zip

Modify your main layout file, views/layouts/main.handlebars, and add the following references:

<link rel=’stylesheet’ href=’/stylesheets/bootstrap.min.css’/>
<link rel=’stylesheet’ href=’/stylesheets/bootstrap-responsive.min.css’/>
<link rel=’stylesheet’ href=’/stylesheets/style.css’/>

5) Install library so you are able to make external requests

$npm install request –save

Looks like that is the equivalent of curl in PHP. Here’s an example of usage:

request(url, function(error, response, contentBody) {
        // Attempt to build the interpoloated address, or fail.
        var address;
        try {
            address = JSON.parse(contentBody).query.results.Result;
            address = Array.isArray(address) ? address[0] : address;
            address = address.line1 + " " + address.line2;
        }
        catch(e) {
            callback("Could not retrieve the location at "+lat+", "+lon);
            return;
        }

        if (error || response.statusCode != 200) {
            callback("Error contacting the reverse geocoding service.");
        }
        else {
            callback(null, address);
        }
    });