Created
May 22, 2015 13:46
-
-
Save MTRNord/dde50c26f9af6be02563 to your computer and use it in GitHub Desktop.
Javascript Problem
This file contains 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
{"message":[{"user":"Chat","text":"Loading emotes and badges.."},{"user":"Chat","text":"Connecting to Night's chat socket server.."},{"user":"Chat","text":"Connected."}]} |
This file contains 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
http = require('http'); | |
fs = require('fs'); | |
beautify = require('js-beautify').js_beautify; | |
fs.writeFile("chat.txt","",function(err) { | |
if(err){ | |
return console.log(err.toString('utf8')); | |
} | |
}); | |
server = http.createServer( function(req, res) { | |
//console.dir(req.param); | |
if (req.method == 'POST') { | |
//console.log("POST"); | |
var body = ""; | |
req.on('data', function (data) { | |
if ( typeof old !== 'undefined' && old ){ | |
if (data == old){ | |
print = 0; | |
}else{ | |
print = 1; | |
//console.log(data.toString('utf8')); | |
body += data; | |
old = body; | |
} | |
}else{ | |
//console.log(data.toString('utf8')); | |
body += data; | |
old = body; | |
print = 1; | |
} | |
}); | |
req.on('end', function () { | |
if (print == 1){ | |
//console.log("file"); | |
var fs = require('fs'); | |
var jsonParsed = JSON.parse(body); | |
var jsonParsed2 = JSON.parse(jsonParsed); | |
fs.writeFile("chat.txt","",function(err) { | |
if(err){ | |
return console.log(err.toString('utf8')); | |
} | |
}); | |
for (var i=0; i<jsonParsed2['message'].length; i++){ | |
//console.log(jsonParsed2['message'][i]); | |
//var writeToFile = beautify(jsonParsed2['message'][i]['user'] + ": " + jsonParsed2['message'][i]['text'], { "wrap_line_length": 20, "indent_size": 0, "end_with_newline": false, "indent_char": "" }) | |
var writeToFile = jsonParsed2['message'][i]['user'] + ": " + jsonParsed2['message'][i]['text'].replace(/.{33}/g, "$&" + "\n"); | |
fs.appendFile("chat.txt", writeToFile + "\n", function(err) { | |
if(err) { | |
return console.log(err.toString('utf8')); | |
} | |
}); | |
} | |
//console.log("Body: " + jsonParsed2); | |
}else{ | |
//console.log("nichts"); | |
} | |
}); | |
res.writeHead(200, {'Content-Type': 'text/html', 'Access-Control-Allow-Origin': '*'}); | |
res.end('post received'); | |
} | |
}); | |
port = 3000; | |
host = '127.0.0.1'; | |
server.listen(port, host); | |
console.log('Listening at http://' + host + ':' + port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment