-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
50 lines (41 loc) · 1.27 KB
/
server.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
////////////////////////////////////////////////////////////////////////////////
// FILE: server.js
// AUTHOR: David Ruvolo
// CREATED: 2019-11-01
// MODIFIED: 2020-11-18
// PURPOSE: server for node.js and express.js application
// DEPENDENCIES: express, bodyParser, node
// STATUS: working
// COMMENTS: NA
////////////////////////////////////////////////////////////////////////////////
// requires
const express = require('express');
const bodyParser = require('body-parser');
const { R } = require("@fridgerator/r-script");
const cors = require("cors");
// define app and set port
const app = express();
const port = process.env.PORT || 5000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
////////////////////////////////////////
// Process User Input
app.post('/data', (req, res) => {
// get input
let power = parseInt(req.body.power);
let input = parseInt(req.body.value);
// process data using js Math.pow()
let squaredJS = Math.pow(input, power);
// run through R script
let r = new R("./R/index.R");
r.data(input, power);
let squaredR = r.callSync();
// send results
res.send({
squaredJS: squaredJS,
squaredR: squaredR,
})
})
// set port
app.listen(port, () => console.log(`Listening on port ${port}`));