基本上是根据Nodejs入门一步步做的,代码也基本一样: index.js:
var server = require('./server.js');
var router = 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(router.route,handle);
server.js:
var http = require("http");
var url = require('url');
exports.start = function(route, handle){
http.createServer(function(request, response){
var pathname = url.parse(request.url).pathname;
route(handle, pathname, response, request);
}).listen(3000);
console.log('Server has started!');
};
router.js:
function route (handle, pathname, response, request) {
console.log('A request for' + pathname);
if(typeof handle[pathname] === 'function'){
handle[pathname](response, request);
}else{
console.log('No request handler for ' + pathname);
response.writeHead(404, {'Content-Type': 'text/plain'});
response.write('404 not found');
response.end();
}
}
exports.route = route;
requestHandler.js:
var querystring = require('querystring'),
fs = require('fs'),
formidable = require('formidable'),
util = require('util');
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" method="post" enctype="multipart/form-data">'+
'<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();
form.uploadDir = 'tmp';
form.parse(request, function(error, fields, files){
//fs.renameSync(files.upload.path, '/tmp/test.png');
try{
console.log(files.upload.path);
}catch(e){
console.log(e);
}
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('/tmp/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;
###报错信息: Cannot read property ‘path’ of undefined
####打印files的结果:
{ upload:
{ domain: null,
_events: {},
_maxListeners: 10,
size: 2722,
path: 'tmp\\2d3ead58151f69465f4fec03b2e27b34',
name: 'image-holder.png',
type: 'image/png',
hash: null,
lastModifiedDate: Tue May 06 2014 16:44:50 GMT+0800 (中国标准时间),
_writeStream:
{ _writableState: [Object],
writable: true,
domain: null,
_events: {},
_maxListeners: 10,
path: 'tmp\\2d3ead58151f69465f4fec03b2e27b34',
fd: null,
flags: 'w',
mode: 438,
start: undefined,
pos: undefined,
bytesWritten: 2722,
closed: true } } }
之前看到有人提过这个问题 链接 ,是因为自己设置了encoding,但是我没设置也是这个问题,各位大神帮忙看一下到底哪里有问题啊
12 回复