Node.js 节点初学者书籍类型错误:无法调用方法';写头';未定义的
我只是跟着Node初学者的书走,同时遇到了一个类型错误。我已经搜索过了,但是没有人能解决这个错误。错误如下所示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
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;