Node Beginner Book TypeError:无法调用方法' writeHead'未定义的

时间:2014-04-14 13:14:25

标签: node.js undefined typeerror

我遇到 TypeError 时,只需按http://www.nodebeginner.org/上的节点初学者书。我已经搜索了但没有人可以解决错误。错误显示如下

D:\delbert\nodejs\proj\requestHandlers.js:7
    response.writeHead(200, {"Content-Type":"text/plain"});
             ^
TypeError: Cannot call method 'writeHead' of undefined
    at D:\delbert\nodejs\proj\requestHandlers.js:7:14
    at ChildProcess.exithandler (child_process.js:635:7)
    at ChildProcess.EventEmitter.emit (events.js:98:17)
    at maybeClose (child_process.js:743:16)
    at Socket.<anonymous> (child_process.js:956:11)
    at Socket.EventEmitter.emit (events.js:95:17)
    at Pipe.close (net.js:465:12)

和源代码:

// // ---------
//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 for " + 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 to route a request for " + pathname);
  if (typeof handle[pathname] === 'function') {
    handle[pathname](response);
  } else {
    console.log("No request handler found for " + 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' was 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' was called.");
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello Upload");
  response.end();
}

exports.start = start;
exports.upload = upload;

我不知道错误是怎么来的。它粘在那里,即使我粘贴了书中的代码。

我尝试了TypeError: Cannot call method 'writeHead' of undefinedhttp://cnodejs.org/topic/4fbca9123a7ec1d151038ac1,但他们没有用。

你能帮帮我吗?

1 个答案:

答案 0 :(得分:4)

这本书确实似乎有些错误。

以下是我发现的一些错误:

  1. server.start()这样调用route()route(handle, pathname, request, response);,但route()实际定义如下:function route(handle, pathname, response)

  2. (很可能是您的特定错误的来源)index.jshandle["/"] = requestHandlers.start();而不是handle["/"] = requestHandlers.start;