arnaucube
arnaucube/comunicationLeapPublic Self-hosted repository
comunicationLeap/node_modules/formidable/tool/record.js
node_modules/formidable/tool/record.jsRaw
 1var http = require('http');
 2var fs = require('fs');
 3var connections = 0;
 4
 5var server = http.createServer(function(req, res) {
 6  var socket = req.socket;
 7  console.log('Request: %s %s -> %s', req.method, req.url, socket.filename);
 8
 9  req.on('end', function() {
10    if (req.url !== '/') {
11      res.end(JSON.stringify({
12        method: req.method,
13        url: req.url,
14        filename: socket.filename,
15      }));
16      return;
17    }
18
19    res.writeHead(200, {'content-type': 'text/html'});
20    res.end(
21      '<form action="/upload" enctype="multipart/form-data" method="post">'+
22      '<input type="text" name="title"><br>'+
23      '<input type="file" name="upload" multiple="multiple"><br>'+
24      '<input type="submit" value="Upload">'+
25      '</form>'
26    );
27  });
28});
29
30server.on('connection', function(socket) {
31  connections++;
32
33  socket.id = connections;
34  socket.filename = 'connection-' + socket.id + '.http';
35  socket.file = fs.createWriteStream(socket.filename);
36  socket.pipe(socket.file);
37
38  console.log('--> %s', socket.filename);
39  socket.on('close', function() {
40    console.log('<-- %s', socket.filename);
41  });
42});
43
44var port = process.env.PORT || 8080;
45server.listen(port, function() {
46  console.log('Recording connections on port %s', port);
47});