-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupload-example1.js
More file actions
33 lines (28 loc) · 1.06 KB
/
upload-example1.js
File metadata and controls
33 lines (28 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
var fs = require('fs')
, http = require('http')
, formidable = require('formidable')
, forHumans = require('./for-humans.js');
http.createServer(function(request, response) {
if (request.url == '/upload' && request.method.toLowerCase() == 'post') {
var form = new formidable.IncomingForm();
form.onPart = function(part) {
// the part is a stream of the incoming upload
var data = '';
part.on('data', function(chunk) {
data += chunk;
});
part.on('end', function() {
fs.writeFile('./files/test.json', data);
});
}
form.on('end', function() {
console.log('mem: ', forHumans(process.memoryUsage().rss));
response.setHeader('Content-Type', 'text/plain; charset=utf-8');
response.end('We got the junk you uploaded. Thanks.');
});
return form.parse(request);
}
response.setHeader('Content-Type', 'text/html; charset=utf-8');
fs.createReadStream('./upload-example-form.html').pipe(response);
}).listen(8080);
console.log('Server ' + process.pid + ' listening on port 8080\n');