Saturday, 19 September 2020

function being called more than once after server recieves data

Here is my code:var http = require('http');var querystring = require('querystring');var readline = require('readline');var messages = "";var serverinput;var postHTML =' Post Example ' +'' +'
' +'Input 1:
' +'

''
' +'';const rl = readline.createInterface({ input: process.stdin,output: process.stdout});//messages variable loads messages back into itself when data is sent to serverhttp.createServer(function (req, res) {res.writeHead(200);var body = "";req.on('data', function (chunk) {body = "";body += chunk;});req.on('end', function () {parsed = querystring.decode(body,null,null);console.log(parsed);console.log("Client: " + parsed.input1);console.log(body);messages = messages + "
User: " + parsed.input1;console.log(messages);res.write(postHTML);res.write(messages);});rl.on('line', function(input) {serverinput = "
Server: " + input;res.write(serverinput);messages = messages + serverinput;console.log(messages);});}).listen(8080);​When I run this, it appears to work perfectly, until the user sends multiple messages without the server responding. This causes the rl.on('line') to get executed more than once. It also just happens randomly occasionally? This causes it to appear like the server had sent the same message multiple times when they had only sent it once. Does anyone know how I could fix this?

Submitted September 20, 2020 at 01:13AM by Grandmaster_Garf

No comments:

Post a Comment