node.js - The Node Beginner Book TypeError: Cannot call method 'writeHead' of undefined -
i follow the node beginner book on http://www.nodebeginner.org/ while encounder typeerror。 i've searched no 1 can solve error. error shows follows
d:\delbert\nodejs\proj\requesthandlers.js:7 response.writehead(200, {"content-type":"text/plain"}); ^ typeerror: cannot call method 'writehead' of undefined @ d:\delbert\nodejs\proj\requesthandlers.js:7:14 @ childprocess.exithandler (child_process.js:635:7) @ childprocess.eventemitter.emit (events.js:98:17) @ maybeclose (child_process.js:743:16) @ socket.<anonymous> (child_process.js:956:11) @ socket.eventemitter.emit (events.js:95:17) @ pipe.close (net.js:465:12)
and source code:
//---------//
//server.js//
//---------//
var http = require("http"); var url = require("url"); function start(route, handle) { function onrequest(request, response) { var pathname = url.parse(request.url).pathname; console.log("request " + pathname + " received."); route(handle, pathname, request, response); } http.createserver(onrequest).listen(8888); console.log("server has started."); } exports.start = start;
//--------//
//index.js//
//--------//
var server = require("./server"); var router = require("./route"); var requesthandlers = require("./requesthandlers"); var handle = {} handle["/"] = requesthandlers.start(); handle["/start"] = requesthandlers.start; handle["/upload"] = requesthandlers.upload; server.start(router.route, handle);
//--------//
//route.js//
//--------//
function route(handle, pathname, response) { console.log("about route request " + pathname); if (typeof handle[pathname] === 'function') { handle[pathname](response); } else { console.log("no request handler found " + pathname); response.writehead(404, {"content-type": "text/plain"}); response.write("404 not found"); response.end(); } } exports.route = route;
//------------------//
//requesthandlers.js//
//------------------//
var exec = require("child_process").exec; function start(response) { console.log("request handler 'start' called."); exec("ls -lah", function (error, stdout, stderr) { response.writehead(200, {"content-type":"text/plain"}); response.write(stdout); response.end(); }); } function upload(response) { console.log("request handler 'upload' called."); response.writehead(200, {"content-type": "text/plain"}); response.write("hello upload"); response.end(); } exports.start = start; exports.upload = upload;
i don't know how error comes. stuck there paste code book.
i tried typeerror: cannot call method 'writehead' of undefined , http://cnodejs.org/topic/4fbca9123a7ec1d151038ac1 did not work.
would please me?
the book indeed seem have errors then.
here couple of errors spotted right away:
server.start()
callsroute()
this:route(handle, pathname, request, response);
,route()
defined this:function route(handle, pathname, response)
(and source of specific error)
index.js
hashandle["/"] = requesthandlers.start();
instead ofhandle["/"] = requesthandlers.start;
Comments
Post a Comment