Skip to content

Instantly share code, notes, and snippets.

@ifukazoo
Created July 8, 2014 13:17
Show Gist options
  • Select an option

  • Save ifukazoo/9895b926c7c7d3def2a3 to your computer and use it in GitHub Desktop.

Select an option

Save ifukazoo/9895b926c7c7d3def2a3 to your computer and use it in GitHub Desktop.
upload
'use strict';
var http = require('http');
var url = require('url');
var mime = require('mime');
var fs = require('fs');
var multiparty = require('multiparty');
var util = require('util');
var port, uploadDir;
if (process.argv.length !== 4) {
console.log("usage:" + process[1] + " <port> <upload directory>");
process.exit(1);
}
port = process.argv[2];
uploadDir = process.argv[3];
var upload_server = http.createServer(function(req, res) {
var pathName = url.parse(req.url)['pathname'];
console.log(req.method, pathName);
if (req.method === 'GET') {
doGet(req, res);
} else if (req.method === 'POST') {
doPost(req, res);
}
});
upload_server.on('clientError', function(e) {
console.log('Client Error: ', e.message);
process.exit(1);
});
upload_server.on('error', function(e) {
console.log('Server Error: ', e.message);
process.exit(1);
});
upload_server.listen(port, function() {
console.log('port = ' + port);
console.log('listening ...');
});
var doGet = function(req, res) {
var pathName = url.parse(req.url)['pathname'];
if (pathName === '/') {
pathName += 'index.html';
}
pathName = '.' + pathName;
console.log(pathName);
fs.exists(pathName, function(exists) {
if (exists) {
doGetFile(pathName, res);
} else {
res.statusCode = 404;
res.writeHead(404, { 'Content-Length': 0, 'Connection': 'close'});
res.end();
}
});
};
var doPost = function(req, res) {
var form = new multiparty.Form({'uploadDir': '.'});
form.on('file', function(name, file) {
console.log('content-type=' + file['headers']['content-type']);
console.log(file['headers']['content-disposition']);
var path = file['path'];
var original = file['originalFilename'];
var rename = function() {
fs.rename(path, uploadDir + '/' + original, function(err) {
if (err) throw err;
});
};
fs.exists(uploadDir, function(exists) {
if (!exists) {
fs.mkdir(uploadDir, '0777', function(err) {
if (err) throw err;
rename();
});
} else {
rename();
}
});
// console.log(JSON.stringify(file));
});
form.parse(req, function(err, fields, files) {
if (err) {
res.writeHead(400, {'content-type': 'text/plain'});
res.end("invalid request: " + err.message);
return;
}
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received fields:\n\n '+util.inspect(fields));
res.write('\n\n');
res.end('received files:\n\n '+util.inspect(files));
});
};
var doGetFile = function(path, res) {
fs.readFile(path, function(e, content) {
if (e) {
res.writeHead(500, e.message,
{ 'Content-Length': 0, 'Connection': 'close'});
res.end();
} else {
res.writeHead(200, {
'Content-Length': content.length,
'Content-Type': mime.lookup(path),
'Connection': 'close'});
res.write(content);
res.end();
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment