Maps example with Google Maps and Nodejs

Originally posted on Prash's Blog:

This is a short piece of code to show maps in a nodejs application.

We will need some 3rd party modules, so install these modules

  • EJS is a dependency for Express.
  • Express is a framework ExpressJS built on top of ConnectJS, it allows robust routing, view rendering, and templating. ExpressJS Details here.
  • GeoHash module is needed to convert geohashes to latitude and longitude coordinates. Geohash is used to uniquely identify a geographical point, it uses latitude and longitude to create a composite string that can be transmitted, stored. Datastores that do not have strong spatial indexing support, geohashes can be used. But having said that, in our application below, there is nothing stopping you from directly entering lat and lon coordinates in url querystring, it is just that geohashes will make the url shorter. Just to note that with a geohash you can trim characters from the right side…

View original 128 more words

Webapplications with node.js – Jade

Originally posted on iPROFS Technology Blog:

In my previous post I described how you can install node.js and how to use express.js to create a webapplication for the node.js server. Now it is time to look into the templating engine – Jade – that express uses for outputting html to the browser.

As I did not have any knowledge yet of jade, it was time for me to do a quick study of jade. As a lot of github projects, the initial documentation is rather sparse, since it only showed an example of the jade template language will be translated into html. But luckily clicking on the github fork ribbon showed some more documentation on github. Basically it looked pretty easy, it’s just html with indentation meaning something and with all the >’s and <‘s. replaced by ‘(‘ and ‘)’. At least that makes it easier to type in html

View original 1,175 more words

Node.JS & MongoDB – Looping through Collection using core MongoDB library

Originally posted on Technology Discussions by Ajay Singh:

As we all know that MongoDB is very good companion to Node.JS because of the linkage with JSON which is common to both the platforms. JSON is an integral part of Node.JS as it is JavaScript and MongoDB stores data in Binary JSON format.

Now the question is how to perform some simple operations like looping through the objects in collection. This article assumes that you have already installed the core MongoDB library using Node Package Manager (NPM).

sudo npm install mongodb

Now the very first step is to declare the variables pointing to the ‘MyDB’ database in MongoDB:


// MongoDB Connection
var Db = require('mongodb').Db,
    Connection = require('mongodb').Connection,
    Server = require('mongodb').Server,
    BSON = require('mongodb').BSONNative;

var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT']…

View original 97 more words

Delegation vs Inheritance in JavaScript

Originally posted on JavaScript, JavaScript...:

When asked what he might do differently if he had to rewrite Java from scratch, James Gosling suggested that he might do away with class inheritance and write a delegation only language.

Using inheritance as a vehicle for code reuse is a bit like ordering a happy meal because you wanted the plastic toy. Sure a circle is a shape and a dog is a mammal – but once we get past those textbook examples most of our hierarchies get arbitrary and tenuous – built for manipulating behaviour even as we pretend we are representing reality. Successive descendants are saddled with an ever increasing number of unexpected or irrelevant behaviours for the sake of re-using a few.

Delegation is a technique that promotes code reuse by allowing runtime function invocation in the context of a specific instance – regardless of the hierarchical lineage of instance and function. JavaScript has…

View original 1,133 more words

6 Months with Node.js

Originally posted on hueniverse:

For the past 6 months I’ve been spending most of my time building a new service called Sled. I’ll write more about Sled in the coming weeks, but for now all you need to know is that Sled is a collaborative list making tool for small groups.

Sled is built entirely in JavaScript, both on the back-end and front-end. The back-end is build using Node.js (aka Node), the relatively new server-side JavaScript platform. There is some published experience available about building web applications and services using Node, but the experience available is focused on using Node for real-time applications like instant messaging, chat rooms, and games. It is unusual to hear someone recommends Node for building a new web site if it is not focused on real-time.

I expect this to change very fast.

While Sled has a bit of real-time functionality in the form of live list updates…

View original 987 more words

Node.js: The Style of Non-Blocking

Originally posted on hueniverse:

This post is part of a series of articles about my recent experience building Sled using Node.js.

Node is all about non-blocking, asynchronous architecture. This means any activity taking a long time to finish, such as file access, network communication, and database operations, are requested and put aside until the results are ready and returned via a callback function. Instead of asking to read a file and waiting for the operating system to come back with a file handler or buffer, the a callback function is invoked when the operation is completed, freeing the server to handle additional requests.

What gives Node a bit of a negative reputation is how this architecture affects its style of programming and the difficulty some people are having in getting used to it. When I first started, I described this convoluted style of coding like scratching your right armpit with your left hand by…

View original 516 more words

NodeJS – more than just a totally awesome web app platform.

Originally posted on milliken:

So I needed to create a monitoring app that would hit a RESTful interface that required authentication. The only way to hit this RESTful service was to POST to a login page and then manage the authentication cookies for the remainder of the session. And I needed to get this done fast.

This is why I love NodeJS. NoneJS is more than a totally awesome web app platform. It makes for a great client application too.

NodeJS for the command line

The following is the basic code for kicking of an https request. The options argument contains the url, method, and any headers (including cookies). The data argument is for any data that needs to be POSTed with the request. (Make sure you set the appropriate headers for the data like content-type).

The magic is in the processResponse function. This is where we capture the cookie and store it for…

View original 40 more words