Created
May 22, 2012 10:45
-
-
Save ashnur/2768269 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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