-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
48 lines (37 loc) · 1.44 KB
/
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
45
46
47
48
const express = require('express');
const { spawn } = require('child_process');
const app = express();
const port = 3000;
const multer = require('multer');
const upload = multer(); // Sets up multer for data parsing
// Express 4.16+ has built-in body-parser middleware for urlencoded form data
app.use(express.urlencoded({ extended: true }));
// Serve static files from 'public' folder
app.use(express.static('public'));
app.post('/predict', upload.none(), (req, res) => {
console.log('Received body:', req.body); // Detailed logging of the body
const userInput = req.body.textInput;
if (typeof userInput === 'undefined') {
console.error('userInput is undefined.');
return res.status(400).send('No textInput provided.');
}
console.log('Received userInput:', userInput); // Log the userInput to see what you received
const pythonProcess = spawn('python3', ['IQ_predict_run.py', userInput]);
let pythonOutput = '';
pythonProcess.stdout.on('data', (data) => {
pythonOutput += data.toString();
});
pythonProcess.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
pythonProcess.on('close', (code) => {
if (code !== 0) {
console.error(`Python script exited with code ${code}`);
return res.send(`Error: Python script exited with code ${code}`);
}
res.send(`Prediction: ${pythonOutput}`);
});
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});