-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.js
39 lines (31 loc) · 1.11 KB
/
node.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
const express = require('express');
const bodyParser = require('body-parser');
const { spawn } = require('child_process');
const app = express();
const port = process.env.PORT || 3000;
// Middleware for parsing JSON and URL-encoded data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Serve static files from the 'public' directory
app.use(express.static('public'));
// Endpoint for chatbot interaction
app.post('/chat', (req, res) => {
const userInput = req.body.message;
const pythonProcess = spawn('python', ['./node.py']); // Adjust path if necessary
pythonProcess.stdin.write(JSON.stringify({ message: userInput }));
pythonProcess.stdin.end();
let dataToSend = '';
pythonProcess.stdout.on('data', function(data) {
dataToSend += data.toString();
});
pythonProcess.on('close', (code) => {
if (code !== 0) {
return res.status(500).send({ error: 'Python script failed' });
}
res.json(JSON.parse(dataToSend));
});
});
// Start the server
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});