Reputation: 175
I am very new to node.js, and was playing around with a few tutorials and stumbled across this problem doing a little refactoring.
The tutorial I was following is linked here: http://www.tutorialspoint.com/nodejs/nodejs_web_module.htm
I decided to split out the callbacks to make the code a little more readable, so created a file reader method and a 'monitor' method:
function monitor(request, response)
{
var pathname = url.parse(request.url).pathname;
fs.readFile(pathname.substr(1), reader() );
}
http.createServer(monitor()).listen(8080);
When I run this I get the following error:
var pathname = url.parse(request.url).pathname;
^
TypeError: Cannot read property 'url' of undefined
at monitor
Clearly this is a type issue. I was looking into casting to http.incomingMessage, but am not familiar enough with javascript and my web searching did not yield a quick solution.
Thanks!
Upvotes: 1
Views: 839
Reputation: 869
Your problem is in this line:
http.createServer(monitor()).listen(8080);
It should be:
http.createServer(monitor).listen(8080);
The reason is because you want to pass the monitor function as a callback, not call it. Placing the parenthesis after monitor
will call the function with no arguments. When arguments are not given to the function, they take on the value undefined
, hence the error you're getting.
Upvotes: 5