Если лениво файлы качать, выложу исходный код:
server.js
"use strict";
var http = require('http');
var debug = require('debug')('server');
var handler = require('./handler');
var server = new http.Server();
server.on('request', handler);
server.listen(3000, function(err) {
if (err) throw err;
debug("listening");
});
handler.js
var debug = require('debug')('server:handler');
var url = require('url');
var config = require('config');
var fs = require('fs');
var path = require('path');
var mime = require('mime');
var Busboy = require('busboy');
module.exports = function(req, res) {
// /page/my.html -> файл
// /page/my.html?test=5 -> файл
var urlParsed = url.parse(req.url);
//console.dir(req);
//console.dir(res);
// ../../../secret.json
// /page/my.html
var pathname = urlParsed.pathname;
var filePath = path.join(config.publicRoot, pathname);
console.log(pathname);
console.log(filePath);
console.log(req.method);
if (!filePath.startsWith(config.publicRoot + path.sep)) {
res.statusCode = 400;
console.log("Bye-bye silly hacker!");
res.end("Bye-bye silly hacker!");
return;
}
var file = new fs.ReadStream( filePath );
if( pathname === '/path/' && req.method.toLowerCase() === 'post' ){
var busboy = new Busboy({ headers: req.headers });
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
console.log( fieldname );
console.log( file );
console.log( filename );
console.log( encoding );
console.log( mimetype );
file.pipe(fs.createWriteStream( filePath ));
});
busboy.on('finish', function() {
res.writeHead(200, { 'Connection': 'close' });
res.end("That's all folks!");
});
return req.pipe(busboy);
}
file.pipe(res);
file.on( 'error', function(err) {
if (err) {
if (err.code == 'ENOENT') {
res.statusCode = 404;
console.log("ENOENT");
res.end("404");
} else {
res.statusCode = 500;
res.end("500");
}
return;
}
res.on( 'close', function(){
file.destroy();
});
});
};
config/default.js
var path = require('path');
module.exports = {
projectRoot: process.cwd(),
publicRoot: process.cwd() + path.sep + 'public'
};
public/page/my.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>Upload or get or delete file</h1>
<form action="" id="file-upload" enctype="multipart/form-data" >
<input id="file" type="file">
<input type="submit" value="Send">
</form>
<button id="get">Get file</button><button id="delete">Delete file</button>
<div id="message"></div>
<script>
var form = document.getElementById('file-upload');
var file = document.getElementById('file');
var getFile = document.getElementById('get');
var deleteFile = document.getElementById('delete');
var message = document.getElementById('message');
getFile.onclick = function( e ){
var xhr = new XMLHttpRequest();
xhr.open('GET', '/path/file.zip', true);
xhr.onload = function ( e ) {
if (xhr.status === 200) {
console.dir(xhr);
console.dir(e);
} else {
message.innerHTML = 'Error, please try later';
}
};
xhr.send(formData);
};
form.onsubmit = function( e ){
var newFile = file.files[0];
var formData = new FormData();
var xhr = new XMLHttpRequest();
message.innerHTML = '';
e.preventDefault();
if (!newFile.name.match('.zip')) {
message.innerHTML = 'Please, upload only ZIP files!';
return;
}
formData.append('zipfile', newFile);
xhr.open('POST', '/path/', true);
xhr.onload = function (data) {
if (xhr.status === 200) {
message.innerHTML = 'Uploaded!';
console.dir( data );
} else {
message.innerHTML = 'Error, please try later';
}
};
xhr.send(formData);
};
</script>
</body>
</html>