Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Loading external libraries
  2. http = require('http');
  3. fs = require('fs');
  4. d3 = require('d3');
  5. pyshell = require('python-shell');
  6. path = require('path');
  7. express = require('express');
  8.  
  9. server = http.createServer( function(req, res) {
  10.  
  11.     //console.dir(req.param);
  12.  
  13.     if (req.method == 'POST') {
  14.         console.log("POST"); //print indicator of POST to stdout
  15.         var body = ''; //beginning of text
  16.  
  17.         req.on('data', //request on data event
  18.                 function (data) {
  19.                     body += data; //print the received data to body
  20.                     console.log("Partial body: " + body); //log to stdout
  21.                 }
  22.         );
  23.  
  24.         req.on('end', function () { //request on end of data receiving
  25.             console.log("Body: " + body); //prints FULL body to stdout
  26.  
  27.             fs.writeFile("test.txt", body, function(err) {
  28.                 if(err) {
  29.                     return console.log(err);
  30.                 }
  31.                 console.log("The file was saved!"); //if successful
  32.             });
  33.         });
  34.         //send a response header to the request
  35.         res.writeHead(200, {'Content-Type': 'text/html'});
  36.         res.end('post received'); //full res (headers/body) sent; msg done
  37.         //process data
  38.         setInterval(function("test.txt") {}, 1000); //once every second for now
  39.  
  40.  
  41.     } else { //not POST
  42.  
  43.         //console.log("GET");
  44.         res.writeHead(200, {'Content-Type': 'text/plain'});
  45.         res.end('Hello World\n');
  46.         //var html = '<html><body><form method="post" action="http://localhost:3000">Name: <input type="text" name="name" /><input type="submit" value="Submit" /></form></body>';
  47.         // var html = fs.readFileSync('index.html');
  48.         // res.writeHead(200, {'Content-Type': 'text/html'});
  49.         // res.end(html);
  50.     }
  51.  
  52. });
  53.  
  54. port = 3007;
  55. host = '0.0.0.0';
  56. server.listen(port, host);
  57. console.log('Listening at http://' + host + ':' + port);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement