Ver código fonte

Basic functionality now works

Fixing-CORS-crash
BinHong Lee 7 anos atrás
pai
commit
8267064a35
3 arquivos alterados com 50 adições e 6 exclusões
  1. +2
    -0
      .gitignore
  2. +43
    -5
      index.js
  3. +5
    -1
      package.json

+ 2
- 0
.gitignore Ver arquivo

@@ -0,0 +1,2 @@
node_modules
package-lock.json

+ 43
- 5
index.js Ver arquivo

@@ -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
}

+ 5
- 1
package.json Ver arquivo

@@ -8,5 +8,9 @@
"start": "node index.js"
},
"author": "",
"license": "MIT"
"license": "MIT",
"dependencies": {
"body-parser": "^1.18.2",
"express": "^4.16.2"
}
}

Carregando…
Cancelar
Salvar