-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
45 lines (31 loc) · 1023 Bytes
/
app.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
const express = require('express');
const bodyParser = require('body-parser'); // To parse HTTP body element
const sh = require('./helpers/async-cmd'); // To execute a shell command
const app = express();
// MIDDLEWARE
app.use(bodyParser.json()); // Parse JSON
// ROUTES
// This listens for GET requests on '/data'
app.get('/data', (req, res) => {
res.send('Here is some data');
});
// This listens for POST requests on '/square'
app.post('/square', async (req, res) => {
// Save body element to variable data
const data = req.body.number;
console.log(res.body);
// The command to run
const cmd = `python program.py -s ${data}`;
// Execute command
try{
let { stdout, stderr } = await sh(cmd);
// Send result
res.send(stdout);
}
catch(err){
// Send error if something fails
res.status(400).send(err);
}
});
// LISTEN ON PORT
app.listen(3000, () => console.log('Example app listening on port 3000!'));