Understanding callbacks in Javascript and node.js


I’m a long time PHP (CodeIgniter & WordPress) developer that only recently wanted to learn a few other languages. I’ve set out to learn Ruby (on Rails, and Sinatra), Python (w/ Flask framework) and Javascript with node.js.

I decided to create the most basic application I can think of, a URL expander, using each of these languages. I have managed to create a working version in every language, except node.js and Javascript.

I kinda know my problem, I know it is related to callbacks. I know I’m not doing it right. I get the basic idea of callbacks, but I just cannot figure out how to fix this mess I have created.

This is my whole code:

var http = require('http');
var url = require('url');
function expand() {
var short = url.parse('http://t.co/wbDrgquZ');
var options = {
host: short.hostname,
port: 80,
path: short.pathname
function longURL(response) {
http.get(options, longURL);

function start() {
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {
"Content-Type": "text/plain"
response.write("Hello World");
console.log("Server has started.");

The server starts, and when a request is made, it calls the expand function which returns the expanded URL in the terminal. I’m trying to get it to print in the browser.

Any help is appreciated. Thanks in advance.

Problem courtesy of: Cem K.


You’ve made a few flaws.

You should rewrite expand to pass the url in and pass a callback in. Any function that does anything asynchronous generally has the signature (data, callback) in node. This basically allows you to say I want this function to do something then tell me when it's done.

function expand(urlToParse, callback) {
// note we pass in the url this time
var short = url.parse(urlToParse);
var options = {
host: short.hostname,
port: 80,
path: short.pathname
// note we store the clientRequest object temporarily
var clientRequest = http.get(options, extractRealURL);

// Always attach the error handler and forward any errors
clientRequest.on("error", forwardError);

function extractRealURL(res) {
callback(null, res.headers.location);

function forwardError(error) {

Here the callback is expected to have the signature of (err, data) which almost all callbacks in node have. We've also added error handling which is a must.

We now change onRequest to actually call expand properly

function onRequest(request, response) {
// parse the incoming url. true flag unpacks the query string
var parsedUrl = url.parse(request.url, true),
// extract the querystring url.
// http://localhost:8888/?url=http://t.co/wbDrgquZ
urlToExpand = parsedUrl.query.url;

// call expand with the url and a callback
expand(urlToExpand, writeResponse);

function writeResponse(error, newUrl) {
// handle the error case properly
if (error) {
response.writeHead(500, { 'Content-Type': 'text/plain'});
// early return to avoid an else block
return response.end(error.message);
response.writeHead(200, { 'Content-Type': 'text/plain'});
// write the new url to the response

Here we have added error handling logic and also unpacked the actual url to expand from the query string.

Generally the pattern of doSomething<data, callback<err, result>> works very well in node.js.

It’s the exact same as let result = doSomething<data> mayThrow err that you expect in your normal blocking languages except it's asynchronous.

Note that the alternative option of passing the ServerResponse object into the function is frowned upon, by doing so your creating unnecessary hard coupling between the expand function and the server response.

The expand function should only expand an url and return the expanded url, it has no business doing IO itself.

Full code

Solution courtesy of: Raynos

View additional discussion.