Node.js -- String returning as undefined?

Hi there,

I’m working through a node.js tutorial.

I’m getting very confused over a function which returns a string, except when its included in another file…

There are 4 scripts:
server.js


var http = require("http");
var url = require("url");

function begin(route, handle){
	http.createServer(function(request, response){
		var pathname = url.parse(request.url).pathname;
		console.log('request for ' + pathname + ' received.');
		
		response.writeHead(200, {"Content-Type": "text/plain"});
		[B]var content = route(handle, pathname); //route returns undefined causing the server to crash.
		response.write(content);[/B]
		response.end();
	}).listen(8080);

	console.log('server has started.');
}

exports.begin = begin;

router.js


function route(handle, pathname){
	console.log('about to route a request for ' + pathname);
	if(typeof handle[pathname] === 'function'){
		handle[pathname]();
	} else {
		console.log("No request handler found for " + pathname);
		return "404 Not found"; //This string returns fine.
	}
}

exports.route = route;

requestHandlers.js


function home(){
	console.log("Request handler 'start' was called."); //I know these functions are being called, because this line is being executed.
	return "This is the start page..."; //Returning undefined.
}

function upload(){
	console.log("Request handler 'upload' was called.");
	return "This is the upload page...";
}

exports.home = home;
exports.upload = upload;

and finally index.js


var server = require('./server');
var router = require('./router');
var requestHandlers = require('./requestHandlers');

var handle = {}
handle["/"] = requestHandlers.home;
handle["/home"] = requestHandlers.home;
handle["/upload"] = requestHandlers.upload;

server.begin(router.route, handle);

Everything is working as it should… except the return from requestHandlers.js is coming through as undefined.

If I call start() or upload() from the requestHandlers.js script and call it directly from the command line, I get a string. But when I stick it in a variable in server.js, I get undefined.

I’m new to node.js, and also fairly new to javascript. I wonder if there’s some quirk of js that’s causing this, or if I’ve just made some elementary mistake.

Thanks in advance for your help.

M

I feel quite the fool!

I just realised in router.js:


function route(handle, pathname){
	console.log('about to route a request for ' + pathname);
	if(typeof handle[pathname] === 'function'){
		handle[pathname]();
	} else {
		console.log("No request handler found for " + pathname);
		return "404 Not found"; //This string returns fine.
	}
}

The issue is I’m not actually returning anything!

Should be:


function route(handle, pathname){
	console.log('about to route a request for ' + pathname);
	if(typeof handle[pathname] === 'function'){
		[B]return handle[pathname]();[/B]
	} else {
		console.log("No request handler found for " + pathname);
		return "404 Not found"; //This string returns fine.
	}
}

Note to administrator: Perhaps you could delete this thread… Seems a waste of database rows!

M