这几天有点儿小空,寻思着看看Node JS 方面的东东。
在网上东搜搜西搜搜,找到如下网站:
http://www.nodebeginner.org/index-zh-cn.html
主题是node 入门。
于是乎就照着上面的代码敲下来, 一路行来倒是没有怎么遇到困难。
唯独最后在引入fs 模块和 formidable模块尝试完成文件上传功能时,出问题了。
报错如下:
fs.renameSync(files.upload.path, "C:/sap/java/nodejs/examples/testUpload/test.png"
^
TypeError: Cannot read property 'path' of undefined
at c:\sap\java\nodejs\examples\hello\requestHandlers.js:30:30
at IncomingForm.<anonymous> (c:\sap\java\nodejs\examples\hello\node_modules\
formidable\lib\incoming_form.js:89:9)
at IncomingForm.EventEmitter.emit (events.js:95:17)
at IncomingForm._error (c:\sap\java\nodejs\examples\hello\node_modules\formi
dable\lib\incoming_form.js:272:8)
at IncomingMessage.<anonymous> (c:\sap\java\nodejs\examples\hello\node_modul
es\formidable\lib\incoming_form.js:119:14)
at IncomingMessage.EventEmitter.emit (events.js:92:17)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
我的代码如下
server.js
var http = require("http");
var url = require("url");
//var formidable = require("formidable");
function start(route,handle) {
http.createServer(function(request, response){
var postData="";
var pathName = url.parse(request.url).pathname;
console.log("Request for " + pathName + " received");
request.setEncoding("utf8");
route(handle, pathName, response, request);
}).listen(8081);
console.log("Server has started");
}
exports.start = start;
requestHandlers.js:
var queryString = require("querystring");
var formidable = require("formidable");
var fs = require("fs");
function start(response, request) {
console.log("Request handler 'start' is called");
var content = '<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"/>' +
'<input type="submit" value="Upload file" />' +
'</form>' +
'</body>' +
'</html>';
response.writeHead(200,{"Content-Type": "Text/html"});
response.write(content);
response.end();
}
function upload(response, request) {
console.log("Request handler 'upload' is called");
var form = new formidable.IncomingForm();
console.log("About to parse");
form.parse(request, function(error, fields, files) {
console.log("parse done");
//try{
fs.renameSync(files.upload.path, "C:/sap/java/nodejs/examples/testUpload/test.png");
/*}catch(e){
console.log(e);
}*/
response.writeHead(200, {"Content-Type": "text/html"});
response.write("received image:<br/>");
response.write("<image src='/show' />");
response.end();
});
}
function show(response, request) {
console.log("Requset handler 'show' is called");
fs.readFile("C:/sap/java/nodejs/examples/testUpload/test.png", "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/png"});
response.write(file, "binary");
response.end();
}
});
}
exports.start = start;
exports.upload = upload;
exports.show = show;
route.js:
function route(handle, pathName, response, request) {
console.log("About to route a request for " + pathName);
if(typeof(handle[pathName])==='function'){
return handle[pathName](response, request);
}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;
index.js:
var server = require("./server.js");
var route = require("./router.js");
var requestHandlers = require("./requestHandlers.js");
var handle = {};
handle['/'] = requestHandlers.start;
handle['/start'] = requestHandlers.start;
handle['/upload'] = requestHandlers.upload;
handle['/show'] = requestHandlers.show;
server.start(route.route, handle);
看到server.js中的红色高亮的那一行,去掉就一切OK了。。。
因为request.setEncoding 这部分node-formidable自身会处理,在这个地方手动给设置一下貌似会有冲突,从而导致出错。
另外,值得注意的两点,在requestHandlers.js中,
1.由于我的开发环境是:win7 64, 在fs.parse 和 fs.readFile的路径参数中,我都是直接给出的是绝对路径。
相对路径没有试过。
2.对应路径下的文件夹必须要存在(fs.renameSync 和 fs.readFile不会自动创建文件夹),否则还是会报错如下:
fs.js:543
return binding.rename(pathModule._makeLong(oldPath),
^
Error: ENOENT, no such file or directory 'X:\...\ddcdf999d6b56cb30716f43e528e7eeb'
at Object.fs.renameSync (fs.js:543:18)
at c:\sap\java\nodejs\examples\hello\requestHandlers.js:30:7
at IncomingForm.<anonymous> (c:\sap\java\nodejs\examples\hello\node_modules\
formidable\lib\incoming_form.js:92:9)
at IncomingForm.EventEmitter.emit (events.js:92:17)
at IncomingForm._maybeEnd (c:\sap\java\nodejs\examples\hello\node_modules\fo
rmidable\lib\incoming_form.js:533:8)
at c:\sap\java\nodejs\examples\hello\node_modules\formidable\lib\incoming_fo
rm.js:211:12
at WriteStream.<anonymous> (c:\sap\java\nodejs\examples\hello\node_modules\f
ormidable\lib\file.js:70:5)
at WriteStream.g (events.js:175:14)
at WriteStream.EventEmitter.emit (events.js:117:20)
at finishMaybe (_stream_writable.js:354:12)
貌似有不少新人在做这个例子的时候都遇到了类似的问题, 如下两个网站中的楼主貌似也都是类似的问题: