Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 节点初学者书籍类型错误:无法调用方法';写头';未定义的_Node.js_Undefined_Typeerror - Fatal编程技术网

Node.js 节点初学者书籍类型错误:无法调用方法';写头';未定义的

Node.js 节点初学者书籍类型错误:无法调用方法';写头';未定义的,node.js,undefined,typeerror,Node.js,Undefined,Typeerror,我只是跟着Node初学者的书走,同时遇到了一个类型错误。我已经搜索过了,但是没有人能解决这个错误。错误如下所示 D:\delbert\nodejs\proj\requestHandlers.js:7 response.writeHead(200, {"Content-Type":"text/plain"}); ^ TypeError: Cannot call method 'writeHead' of undefined at D:\delbert\no

我只是跟着Node初学者的书走,同时遇到了一个类型错误。我已经搜索过了,但是没有人能解决这个错误。错误如下所示

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)
//--------//
//index.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;
//--------//
//route.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;
//------------------//
//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;
我不知道错误是怎么来的。即使我粘贴了书上的代码,它也卡在那里了

我试过了,但没有成功


你能帮帮我吗?

那本书似乎确实有一些错误

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

  • server.start()
    调用
    route()
    如下:
    route(句柄、路径名、请求、响应)
    ,但是
    route()
    实际上是这样定义的:
    函数路由(句柄、路径名、响应)

  • (很可能是您的特定错误的来源)
    index.js
    具有
    handle[“/”]=requestHandlers.start()而不是
    句柄[“/”]=requestHandlers.start


  • 我在index.js中将route函数的定义修改为
    函数route(句柄、路径名、请求、响应)
    。不带
    ()
    的启动方法可以。谢谢。@Delbert-你有没有看过教程中最后一个用例(上传一个.png图像文件)?我想我已经完成了所有的步骤,但它不起作用。我可以发布一个问题,但我需要有经验的人与本教程。你能帮忙吗?
    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;