-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
73 lines (66 loc) · 1.83 KB
/
server.js
File metadata and controls
73 lines (66 loc) · 1.83 KB
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
import express from 'express';
import path from 'path';
import cors from 'cors';
import { dataForChart, recursiveClassifying, tree } from './src/ScoringSystem';
import initializeDbPostgres from './src/config/postgres';
import TrainData from './src/models/trainData';
const app = express();
const root = path.join(__dirname, 'client', 'build');
app.use(express.static(root));
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
let data, decisionTree;
initializeDbPostgres(async () => {
data = await TrainData.findAll({ raw: true });
console.log('Data for training is fetched');
decisionTree = tree(data);
console.log('Decision tree ready');
});
const validator = async function(req, res, next) {
const { income, amount, history, coapplicantIncome } = req.body;
if (history === 0 || amount >= 1000 || (income <= 100 && coapplicantIncome <= 100))
return res.send(false);
else if (income >= 50000 || coapplicantIncome >= 50000 || amount <= 2)
return res.send(true);
return next();
};
app.get('/data', async function(req, res) {
try {
res.send(dataForChart(decisionTree));
} catch (err) {
res.sendStatus(403);
}
});
app.post('/data', validator, async function(req, res) {
try {
const {
income,
amount,
coapplicantIncome,
education,
dependents,
selfEmployed,
history,
married
} = req.body;
const decision =
recursiveClassifying(
[
married,
dependents,
education,
selfEmployed,
income,
coapplicantIncome,
amount,
history
],
decisionTree
) === 1;
res.send(decision);
} catch (err) {
res.sendStatus(400);
}
});
app.listen(process.env.PORT || 3001, () => console.log('Server is running on port 3001'));