Skip to content

Instantly share code, notes, and snippets.

@ashnur
Created May 22, 2012 10:45
Show Gist options
  • Save ashnur/2768269 to your computer and use it in GitHub Desktop.
Save ashnur/2768269 to your computer and use it in GitHub Desktop.
server.coffee server.js X
fs = require('fs');
ecstatic = (require('ecstatic'))(__dirname);
uri = require('URIjs');
async = require('async');
routes = function(req, res) {
var form;
form = new formidable.IncomingForm;
if (req.url === '/upload' && req.method.toLowerCase() === 'post') {
return async.auto({
readUploadedFile: function(cb) {
return form.parse(req, function(err, fields, files) {
return cb(err, files);
});
},
parseUploadedFile: [
'readUploadedFile', function(cb, r) {
return fs.readFile(r.readUploadedFile.bookmars.path, 'utf8', cb);
}
],
findUrls: [
'parseUploadedFile', function(cb, r) {
var matches;
matches = r.parseUploadedFile.match(uri.find_uri_expression);
return async.map(matches, function(path, cb) {
return cb(null, uri(path));
}, cb);
}
],
filterUrls: [
'findUrls', function(cb, r) {
return async.filter(r.findUrls, function(url, cb) {
return cb(url._parts.protocol != null);
}, function(r) {
return cb(null, r);
});
}
],
respond: [
'filterUrls', function(err, r) {
res.writeHead(200, {
'content-type': 'application/json'
});
return res.end(JSON.stringify(r.filterUrls, 'utf8'));
}
]
});
} else {
return ecstatic(req, res);
}
};
server = http.createServer(routes);
server.listen(8001);
console.log('Listening on :8001');
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment