From 8267064a350829a510d09f066c665b45ea724944 Mon Sep 17 00:00:00 2001 From: BinHong Lee Date: Fri, 20 Oct 2017 23:59:57 -0500 Subject: [PATCH] Basic functionality now works --- .gitignore | 2 ++ index.js | 48 +++++++++++++++++++++++++++++++++++++++++++----- package.json | 6 +++++- 3 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d5f19d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +package-lock.json diff --git a/index.js b/index.js index 62c9996..aeb66a6 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,44 @@ -var http = require('http') +var bodyParser = require('body-parser') +var express = require('express') -http.createServer(function (request, response) { - response.writeHead(200, {'Content-Type': 'text/plain'}) - response.end('Breakup\n') -}).listen(process.env.PORT) +var app = express() +var input + +app.use(bodyParser.json({ + limit: '100mb', + type: 'application/json' +})) +app.get('/help', function (req, res) { + res.send('https://github.com/binhonglee/Breakups') +}) +app.post('/total', function (req, res) { + input = req.body + var toReturn = { 'total': 0 } + toReturn.total = total(input.users) + res.json(toReturn) +}) +app.post('/perPerson', function (req, res) { + input = req.body + var toReturn = { 'perPerson': 0 } + toReturn.perPerson = (total(input.users)) / input.users.length + res.json(toReturn) +}) +app.post('/oweChart', function (req, res) { + input = req.body + var perPerson = total(input.users) / input.users.length + for (var i = 0; i < input.users.length; i++) { + input.users[i].amount -= perPerson + } + res.json(input) +}) +// app.listen(3000) +app.listen(process.env.PORT) + +function total (input) { + var total = 0 + for (var i = 0; i < input.length; i++) { + total += input[i].amount + } + + return total +} diff --git a/package.json b/package.json index 056c6c0..71c3c6d 100644 --- a/package.json +++ b/package.json @@ -8,5 +8,9 @@ "start": "node index.js" }, "author": "", - "license": "MIT" + "license": "MIT", + "dependencies": { + "body-parser": "^1.18.2", + "express": "^4.16.2" + } }