I think I am just missing something basic. I dont understand the how memory is being allocated. If I just hit this function over and over again, it leaks like crazy (just watching it grow in top). I am new to nodejs and javascript in general. I can't seem to find any explanation why this is wrong, but it is wrong.. Is it how I am calling handleMessage by using the require statement inline? I also tried setting a variable equal to the require statement and just accessed that variable but it still kept eating memory. Anything will be a big help!
main.js:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
if (req.url == "/getRandom")
{
require('./getRandom.js').handleMessage(req,res)
}
else
{
res.end(req.url+'\n');
}
}).listen(443);
console.log('Server running at http://127.0.0.1:443/');
getRandom.js:
var qs = require('querystring');
function getRandom()
{
var numbers = new Array()
for (i = 0; i < 100; i++)
{
numbers[i] = Math.floor(Math.random()*100);
}
return numbers;
}
function handleMessage(req,res)
{
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
var t = JSON.parse(body);
var c = t.name;
var response = new Object();
response.numbers = getRandom();
res.end (JSON.stringify(response));
response.numbers = null;
response = null;
body=null;
t=null;
});
}
module.exports.handleMessage = handleMessage;