I was learning "The Node beginner book" and doing the practice in the book, the practice is to present a picture which was uploaded by user. it's an example wrote with node-formidable, code as below:
var formidable = require('formidable'),
http = require('http'),
util = require('util');
http.createServer(function(req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
// parse a file upload
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
return;
}
// show a file upload form
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" '+
'method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
}).listen(8888);
I run it with node filename.js, then I open my browser located to http://localhost:8888/upload, comes something as below:
I enter a name and choose a file, then it comes as below:
I click the upload button, the response as below:
received upload:
{ fields: { title: 'Hello Wolrd' },
files:
{ upload:
File {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
size: 37417,
path: '/tmp/upload_a306115e1e630a0c548b6d820fe803cb',
name: 'myfile_icon_file_4.png',
type: 'image/png',
hash: null,
lastModifiedDate: 2016-10-11T03:52:41.052Z,
_writeStream: [Object] } } }
how to get the property path? why does a word File created there?


formidablelibrary. An instance ofFile. You can access the path via:files.upload.pathTypeError: Cannot read property 'path' of undefined, how to?