跟随:节点初学者书
我无法调试此问题或在线找到解决方案。Node.js 的新手,希望有人能提供解决方案
错误:已更新控制台日志信息 2012 年 2 月 11 日星期六上午 7:27:17
Request for/ received!
About to route a request for /
Request handler 'start' was called.
Request for/favicon.ico received!
About to route a request for /favicon.ico
No request handler found for /favicon.ico
Request for/favicon.ico received!
About to route a request for /favicon.ico
No request handler found for /favicon.ico
Request for/upload received!
About to route a request for /upload
Request handler 'upload' was called.
about to parse
{ output: [],
outputEncodings: [],
writable: true,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: true,
_hasBody: true,
_trailer: '',
finished: false,
socket:
{ _handle:
{ writeQueueSize: 0,
socket: [Circular],
onread: [Function: onread] },
_pendingWriteReqs: 0,
_flags: 0,
_connectQueueSize: 0,
destroyed: false,
bytesRead: 66509,
bytesWritten: 638,
allowHalfOpen: true,
writable: true,
readable: true,
server:
{ connections: 1,
allowHalfOpen: true,
_handle: [Object],
_events: [Object],
httpAllowHalfOpen: false },
ondrain: [Function],
_idleTimeout: 120000,
_idleNext:
{ _idleNext: [Circular],
_idlePrev: [Circular],
ontimeout: [Function] },
_idlePrev:
{ _idleNext: [Circular],
_idlePrev: [Circular],
ontimeout: [Function] },
_idleStart: Sat, 11 Feb 2012 15:25:28 GMT,
_events: { timeout: [Function], error: [Function], close: [Object] },
ondata: [Function],
onend: [Function],
_httpMessage: [Circular] },
connection:
{ _handle:
{ writeQueueSize: 0,
socket: [Circular],
onread: [Function: onread] },
_pendingWriteReqs: 0,
_flags: 0,
_connectQueueSize: 0,
destroyed: false,
bytesRead: 66509,
bytesWritten: 638,
allowHalfOpen: true,
writable: true,
readable: true,
server:
{ connections: 1,
allowHalfOpen: true,
_handle: [Object],
_events: [Object],
httpAllowHalfOpen: false },
ondrain: [Function],
_idleTimeout: 120000,
_idleNext:
{ _idleNext: [Circular],
_idlePrev: [Circular],
ontimeout: [Function] },
_idlePrev:
{ _idleNext: [Circular],
_idlePrev: [Circular],
ontimeout: [Function] },
_idleStart: Sat, 11 Feb 2012 15:25:28 GMT,
_events: { timeout: [Function], error: [Function], close: [Object] },
ondata: [Function],
onend: [Function],
_httpMessage: [Circular] },
_events: { finish: [Function] } }
/usr/local/lib/node_modules/formidable/lib/incoming_form.js:247
undefined
if (this.headers['content-length']) {
^
TypeError: Cannot read property 'content-length' of undefined
at IncomingForm._parseContentLength (/usr/local/lib/node_modules/formidable/lib/incoming_form.js:247:19)
at IncomingForm.writeHeaders (/usr/local/lib/node_modules/formidable/lib/incoming_form.js:126:8)
at IncomingForm.parse (/usr/local/lib/node_modules/formidable/lib/incoming_form.js:80:8)
at Object.upload [as /upload] (/Applications/MAMP/htdocs3/js/nodejs/webapp/requestHandlers.js:34:8)
at route (/Applications/MAMP/htdocs3/js/nodejs/webapp/router.js:4:20)
at Server.onRequest (/Applications/MAMP/htdocs3/js/nodejs/webapp/server.js:20:3)
at Server.emit (events.js:70:17)
at HTTPParser.onIncoming (http.js:1511:12)
at HTTPParser.onHeadersComplete (http.js:102:31)
at Socket.ondata (http.js:1407:22)
结束错误
requestHandlers.js
var querystring = require("querystring"),
fs = require("fs"),
formidable = require("formidable");
function start(response) {
console.log("Request handler 'start' was called.");
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload" enctype="multipart/form-data" '+
'method="post">'+
'<input type="file" name="upload" multiple="multiple">'+
'<input type="submit" value="Upload file" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
}
function upload(response, request) {
console.log("Request handler 'upload' was called.");
var form = new formidable.IncomingForm();
console.log("about to parse");
form.parse(request, function(error, fields, files) {
console.log("parsing done");
/*
* Some systems [Windows] raise an error when you attempt to rename new file into one that already exists.
* This call deletes the previous .PNG image prior to renaming the new one in its place.
*/
fs.unlinkSync(__dirname +"/tmp/test.jpg");
fs.renameSync(files.upload.path, "/tmp/test.jpg");
response.writeHead(200, {"Content-Type": "text/html"});
response.write("received image:<br/>");
response.write("<img src='/show' />");
response.end();
});
}
function show(response) {
console.log("Request handler 'show' was called.");
fs.readFile(__dirname + "/tmp/test.jpg", "binary", function(error, file) {
if(error) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(error + "\n");
response.end();
} else {
response.writeHead(200, {"Content-Type": "image/jpg"});
response.write(file, "binary");
response.end();
}
});
}
exports.start = start;
exports.upload = upload;
exports.show = show;
index.js
var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");
var handle = {}
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
handle["/show"] = requestHandlers.show;
server.start(router.route, handle);
路由器.js
function route(handle, pathname, response, request) {
console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') {
handle[pathname](response, request);
} else {
console.log("No request handler found for " + pathname);
response.writeHead(404, {"Content-Type": "text/html"});
response.write("404 Not found");
response.end();
}
}
exports.route = route;
服务器.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, response, request);
}
// http.createServer(onRequest).listen(8888);
// console.log("Server has started.");
http.createServer(onRequest).listen(1337, "127.0.0.1");
console.log('Server Has Started!');
}
exports.start = start;