【问题标题】:The Node Beginner Book TypeError: Cannot call method 'writeHead' of undefinedNode Beginner Book TypeError:无法调用未定义的方法'writeHead'
【发布时间】:2014-05-28 11:19:27
【问题描述】:

我只是在http://www.nodebeginner.org/上关注The Node Beginner Book,同时遇到了一个TypeError。我已经搜索过,但没有人能解决这个错误。错误显示如下

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,但它们不起作用。

你能帮帮我吗?

【问题讨论】:

    标签: node.js undefined typeerror


    【解决方案1】:

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

    以下是我立即发现的几个错误:

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

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

    【讨论】:

    • 我在index.js中将路由函数的定义修改为function route(handle, pathname, request, response)。没有() 的启动方法是可以的。谢谢。
    • @Delbert - 你有没有通过教程中的最后一个用例(上传 .png 图像文件)?我想我已经完成了所有步骤,但它不起作用。我可以发布一个问题,但我需要有本教程经验的人。你能帮忙吗?
    猜你喜欢
    • 1970-01-01
    • 2013-11-10
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多