-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
45 lines (39 loc) · 1.24 KB
/
server.js
File metadata and controls
45 lines (39 loc) · 1.24 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
const http = require('http')
const fs = require('fs')
const { parse } = require('querystring')
const { spawn } = require('child_process');
const server = http.createServer((request, res) => {
if (request.method === 'POST') {
// Parse the data
collectData(request, result => {
// Print ok and log the data as a object
console.log(result)
res.end(result)
})
}
else
{
// Read the web page and print
fs.readFile('./pagina.html', function(err, data) {
if(err) {
res.statusCode = 500
res.end(`Error getting the file: ${err}.`)
} else {
res.writeHead(200, { 'Content-Type': 'text/html' })
res.end(data)
}
})
}
});
function collectData(request, callback) {
let body = ''
request.on('data', chunk => body += chunk.toString())
request.on('end', () => {
let parsedData = parse(body)
// callback(parsedData)
const pythonProcess = spawn('python', ['./python/MHP.py', JSON.stringify(parsedData)])
pythonProcess.stdout.on('data', data => callback(data.toString()))
})
}
server.listen(3000);
console.log("listening 3000")