forked from heroku/node-js-getting-started
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
75 lines (62 loc) · 2.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var express = require('express');
var fs = require('fs');
var app = express();
var cors = require('cors')
var marked = require('marked');
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.use(cors());
app.options('*', cors());
// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.get('/', function(request, response) {
response.render('index', {body: marked(fs.readFileSync('README.md').toString())});
});
app.get('/api/v1', function (req, res) {
res.json({
methods: {
application: {
constraints: {
method: 'GET',
url: req.protocol + '://' + req.get('host') + '/api/v1/application/constraints',
params: {},
},
'first-loan-offer': {
method: 'GET',
url: req.protocol + '://' + req.get('host') + '/api/v1/application/first-loan-offer',
params: {amount: 'Integer', term: 'Integer'}
}
}
}
})
})
app.get('/api/v1/application/constraints', function (req, res) {
res.json({
amountInterval: {min: 10, max: 2000, step: 10, defaultValue: 400}, // IntervalBean:BigDecimal Scrollable Amount interval
termInterval: {min: 3, max: 30, step: 1, defaultValue: 15}, // IntervalBean:Integer Scrollable term interval
})
})
function calculateOffer(req, res) {
var amount = req.query.amount;
var term = req.query.term;
if (amount && term)
res.json({
totalPrincipal: amount,
term: term,
totalCostOfCredit: amount / 10,
totalRepayableAmount: amount * 1.2,
monthlyPayment: amount * 1.2 / term
})
else
res.status(400).json({error: 'Please provide amount and term in query parameters', received_only: {amount: amount, term: term}})
}
app.get('/api/v1/application/first-loan-offer', function (req, res) {
calculateOffer(req, res)
})
app.get('/api/v1/application/real-first-loan-offer', function (req, res) {
setTimeout(function () { calculateOffer(req, res) }, Math.random() * 1000)
})
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});