<repo_name>nathan25maloney/FriendFinder<file_sep>/server.js
var express = require("express");
var bodyParser = require("body-parser");
var path = require("path");
var fs = require("fs");


var app = express();
var PORT = process.env.PORT || 3000;


app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(bodyParser.text());
app.use(bodyParser.json({
  type: "application/vnd.api+json"
}));

function recursiveRoutes(folderName) {
    fs.readdirSync(folderName).forEach(function(file) {
        var fullName = path.join(folderName, file);
        var stat = fs.lstatSync(fullName);
        if (stat.isDirectory()) {
            recursiveRoutes(fullName);
        } else if (file.toLowerCase().indexOf('.js')) {
        	console.log("require('" + fullName + "')");
            require('./' + fullName)(app);    
        }
    });
}
recursiveRoutes('app/routing'); 


app.listen(PORT, function() {
  console.log("App listening on PORT " + PORT);
});<file_sep>/app/routing/apiRoutes.js
var path = require("path");
var fs = require("fs");


module.exports = function(app){
	
    app.get("/api/friends", function(req, res) {
    	let myJson = null;
    	try {
    		myJson = JSON.parse(fs.readFileSync(path.join(__dirname, "../data/friends.json"), 'utf8'));
    	} catch(e){

    	}
		
				
	  	res.send(myJson);
	  	
	});

	app.post("/api/friends", function(req, res) {
		let myJson = null;
		let closestMatch;
		let lowestScore = 100;
		let newFriend = {
			name: req.body.name,
			photo: req.body.photo,
			scores: req.body.scores
		}
		let currentScore;

		try {
    		myJson = JSON.parse(fs.readFileSync(path.join(__dirname, "../data/friends.json"), 'utf8'));
    	} catch(e){
    		myJson = []
    	}

		if(myJson !== undefined){
			
			for (let i = 0; i < myJson.length; i++) {
				currentScore = compareScores(newFriend.scores,myJson[i].scores)
				if(currentScore < lowestScore){
					lowestScore = currentScore;
					closestMatch = myJson[i];

				}

			}
			if(lowestScore < 100){
				console.log(closestMatch);
				closestMatch.matchScore = lowestScore;
				res.send(closestMatch);
				delete closestMatch['matchScore'];
			}
			
		}else {

		}
		


		myJson.push(newFriend);
	  	fs.writeFile(path.join(__dirname, "../data/friends.json"), JSON.stringify(myJson, null, 2) , 'utf-8');
	});

	let compareScores = (newArr,oldArr) => {
		var totalDiff = 0;
		for (let i = 0; i < oldArr.length; i++) {
			totalDiff += Math.abs(newArr[i] - oldArr[i]);	
		}
		console.log(totalDiff);
		return totalDiff;
	}
}<file_sep>/README.md
# FriendFinder

This application is a simple match making site for friends.  It is hosted on the web with heroku and stores a database with all the people who have entered in the answers to their survey.  If there is more than one entry in the database it will take the scores from the survey and match it with the closes answers from another in the database.  It will return that match with the persons photo, name and their match score.  The lowest possible match score the better.  

Future fixes would be to add exception handling that would not match someone with themselves if they take the test more than once.
