// grab the packages we need
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var path = require('path');

app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({
	  extended: true
}));

//app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
var port = process.env.PORT || 8080;

var name;
var private_ip;
var software;

app.use(express.static(path.join(__dirname, 'public')));

var node_list = [];
node_list.push({'ip':'10.2.0.252', 'active_downloaders':0});

app.get('/apps', function(req, res) {
	var application_info = {"software": [{"clean_name": "Android Studio", "id" : 1, "name": "android-studio-ide-145.3360264-linux.zip"}, {"clean_name": "JRE 1.8", "id":2 ,"name": "jre-8u121-linux-i586.tar.gz"} , {"clean_name": "Postman", "id":3 ,"name":"Postman-linux-x64-4.9.3.tar.gz"} , {"clean_name": "AuthPy", "id":4 ,"name":"authy-authy-python-f085687.zip"} , {"clean_name": "MonoDevelop", "id":5 ,"name":"monodevelop-6.1.2.44-1.flatpak"} , {"clean_name": "SimpleSMS", "id":6,"name":"simpleSMS-master.zip"} , {"clean_name": "Ngrok x64", "id":7 ,"name":"ngrok-stable-linux-amd64.zip"} , {"clean_name": "Java OCR", "id":8 ,"name":"javaocr-20100605.zip"} , {"clean_name": "JDK 1.8", "id":9 ,"name":"jdk-8u121-linux-i586.tar.gz"}, {"clean_name": "Android Bundle", "id":10, "name":"android_bundle.zip"} ]};
	res.send(application_info);	
});


app.get('/init_node', function(req, res) {
	// sends back what the root node is when a new node joins the cluster
	// this lets the new node download all current files from the original root node
	res.send(node_list[0]['ip']);
});
app.post('/register_node', function(req, res) {
	node_list.push({'ip':req.body['ip'], 'active_downloaders':0});
	console.log(node_list);
	res.send('success');

});


// start the server
app.listen(8080);
console.log('Server started! At http://localhost:' + port);

@@ -559,16 +559,132 @@
 rs':0%7D);
+%0A//node_list.push(%7B'ip':'1.252', 'active_downloaders':2%7D);%0A//node_list.push(%7B'ip':'1.999', 'active_downloaders':5%7D);
 %0A%0Aapp.ge
@@ -1786,16 +1786,17 @@
 %5D);%0A%7D);%0A
+%0A
 app.post
@@ -1952,16 +1952,937 @@
 ;%0A%0A%7D);%0A%0A
+app.get('/get_ip', function(req, res) %7B%0A%09// find ip with least amount of active downloaders%0A%09min = node_list%5B0%5D%5B%22active_downloaders%22%5D;%0A%09min_ip = node_list%5B0%5D%5B%22ip%22%5D;%0A%09for (var i = 1, len = node_list.length; i %3C len; i++) %7B%0A%09%09if(node_list%5Bi%5D%5B%22active_downloaders%22%5D %3C min)%7B%0A%09%09%09min = node_list%5Bi%5D%5B%22active_downloaders%22%5D;%0A%09%09%09min_ip = node_list%5Bi%5D%5B%22ip%22%5D;%0A%09%09%7D%0A%09  %7D%0A%09console.log(min, min_ip);%0A%09res.send(min_ip);%0A%09%0A%7D);%0A%0Aapp.post('/subtract_downloading_user', function(req, res) %7B%0A%09var arrayFound = node_list.filter(function(node_list) %7B%0A%09%09    return node_list.ip == req.body%5B'ip'%5D;%0A%09%7D);%0A%09arrayFound%5B0%5D%5B'active_downloaders'%5D--;%0A%09console.log(node_list);%0A%09res.send('success');%0A%7D);%0A%0Aapp.post('/add_downloading_user', function(req, res) %7B%0A%09var arrayFound = node_list.filter(function(node_list) %7B%0A%09%09    return node_list.ip == req.body%5B'ip'%5D;%0A%09%7D);%0A%09arrayFound%5B0%5D%5B'active_downloaders'%5D++;%0A%09console.log(node_list);%0A%09res.send('success');%0A%7D);%0A
 %0A// star

load balancing working correctly