Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Loading external libraries
- http = require('http');
- fs = require('fs');
- d3 = require('d3');
- pyshell = require('python-shell');
- path = require('path');
- express = require('express');
- server = http.createServer( function(req, res) {
- //console.dir(req.param);
- if (req.method == 'POST') {
- console.log("POST"); //print indicator of POST to stdout
- var body = ''; //beginning of text
- req.on('data', //request on data event
- function (data) {
- body += data; //print the received data to body
- console.log("Partial body: " + body); //log to stdout
- }
- );
- req.on('end', function () { //request on end of data receiving
- console.log("Body: " + body); //prints FULL body to stdout
- fs.writeFile("test.txt", body, function(err) {
- if(err) {
- return console.log(err);
- }
- console.log("The file was saved!"); //if successful
- });
- });
- //send a response header to the request
- res.writeHead(200, {'Content-Type': 'text/html'});
- res.end('post received'); //full res (headers/body) sent; msg done
- //process data
- setInterval(function("test.txt") {}, 1000); //once every second for now
- } else { //not POST
- //console.log("GET");
- res.writeHead(200, {'Content-Type': 'text/plain'});
- res.end('Hello World\n');
- //var html = '<html><body><form method="post" action="http://localhost:3000">Name: <input type="text" name="name" /><input type="submit" value="Submit" /></form></body>';
- // var html = fs.readFileSync('index.html');
- // res.writeHead(200, {'Content-Type': 'text/html'});
- // res.end(html);
- }
- });
- port = 3007;
- host = '0.0.0.0';
- server.listen(port, host);
- console.log('Listening at http://' + host + ':' + port);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement