Node.js Module – exports vs module.exports

What is the difference between exports and module.exports in Node.js?

You must be familiar with the exports object in Node.js modules, using which you create functions in your modules like this (assume in a file named rocker.js):

exports.name = function() {
    console.log('My name is Lemmy Kilmister');
};

which you call from another file thus: Continue reading

Writing for node and the browser

Modules that can be used both on the server and the client-side are a useful way to reuse code. This is one way of writing a module that will work well with both systems, while allowing you to write code in the familiar node style.

In node you use module.exports (or just exports) to expose a function: Continue reading

JavaScript Module Pattern: In-Depth

The module pattern is a common JavaScript coding pattern. It’s generally well understood, but there are a number of advanced uses that have not gotten a lot of attention. In this article, I’ll review the basics and cover some truly remarkable advanced topics, including one which I think is original.

The Basics

We’ll start out with a simple overview of the module pattern, which has been well-known since Eric Miraglia (of YUI) firstblogged about it three years ago. If you’re already familiar with the module pattern, feel free to skip ahead to “Advanced Patterns”. Continue reading

Creating a JavaScript Object with a Dynamic Property Name

After going crazy trying to dynamically push objects into an JavaScript array I hit another wall. Dynamic object property names. For example, how do you dynamically write myObject.dynamicName?

Well, after much googling I remembered that magical function eval(). The best way, so far, to write dynamic property names is to parse,eval(), a string name into the property definition. For example:

myObject = [];
propertyName="crazyHorse";
propertyValue="David"; 
eval("myObject."+propertyName+"='"+propertyValue+"'");
alert(myObject.crazyHorse);
// Alerts "David"

Making HTTP Requests in Node.js

In previous articles, I’ve focused on creating a Node.js server to handle HTTP requests. This article looks at the problem in reverse, by showing you how to make HTTP requests from your Node.js applications. You may be asking yourself why you would want to do that. Two applications come to mind right away –web scraping and proxying. Scrapers are pieces of software which download web pages and programatically extract information from them. Proxy servers act as intermediaries, forwarding client requests to other servers and returning the responses.

The Request Module

The simplest way to create HTTP requests in Node.js is by using the request module. Written by Mikeal Rogersrequest allows you to make all types of HTTP requests, including GETPOSTPUT, and DELETE. Its flexibility makes the request module ideal for interacting with RESTful APIs. You can install request using the following npm command. Continue reading

AngularJS : AJAX and REST

Although with AngularJS you can develop applications without access to the server, you will be quickly limited when it is necessary to store information. Communication with a server is essential and AngularJS provides two services for this:

Moreover, to facilitate testing there’s 2 $httpBackend objects (one for unit tests, one for functional tests) to simulate server’s behavior. Continue reading

An introduction to AngularJS

When I came across AngularJS a couple weeks ago, I was intrigued at first. By the time I had worked through the set of tutorials available on the AngularJS website, I was thrilled to have found this framework.

What is AngularJS? AngularJS is the (relatively) new kid on the coding block. To quote from their website it is “a structural framework for dynamic web apps” which is especially well-suited for building one-page web apps, although it’s certainly not limited to that. Continue reading

JavaScript Module Pattern: In-Depth

The module pattern is a common JavaScript coding pattern. It’s generally well understood, but there are a number of advanced uses that have not gotten a lot of attention. In this article, I’ll review the basics and cover some truly remarkable advanced topics, including one which I think is original.

The Basics

We’ll start out with a simple overview of the module pattern, which has been well-known since Eric Miraglia (of YUI) firstblogged about it three years ago. If you’re already familiar with the module pattern, feel free to skip ahead to “Advanced Patterns”. Continue reading

Javascript module pattern: When to use it and why

There have been a few criticisms of the javascript module pattern, despite its many advantages. Take for example Jonathon Snook’s “Why I don’t like javascript’s module pattern”. While everyone has their own style, my fear is that people, given Snook’s demanding presence, will take his word for gospel without understanding it.

This article isn’t about teaching you the pattern, but more about when to use it and why—though I’ll start with a brief example of the pattern to get us all on the same page. Continue reading