From 4b27d1854e2db9cc8573e499d9231955c4f39312 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Sun, 23 Feb 2025 08:18:21 +0530 Subject: [PATCH] Added server.js for node server. References python app.py code, uses POST req. --- models/server.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 models/server.js diff --git a/models/server.js b/models/server.js new file mode 100644 index 0000000..c1a65c9 --- /dev/null +++ b/models/server.js @@ -0,0 +1,37 @@ +// server.js +const express = require('express'); +const { spawn } = require('child_process'); +const path = require('path'); + +const app = express(); +const PORT = 3000; + +// Endpoint to run the Python script +app.get('/app.py', (req, res) => { + const imagePath = path.join(__dirname, '/path/to/file'); // Path to your image + + // Spawn a new Python process + const pythonProcess = spawn('python', ['app.py', imagePath]); + + // Collect data from the script + pythonProcess.stdout.on('data', (data) => { + console.log(`Output: ${data}`); + res.send(data.toString()); // Send the output back to the client + }); + + // Handle errors + pythonProcess.stderr.on('data', (data) => { + console.error(`Error: ${data}`); + res.status(500).send(data.toString()); + }); + + // When the process is done + pythonProcess.on('close', (code) => { + console.log(`Python process exited with code ${code}`); + }); +}); + +app.listen(PORT, () => { + console.log(`Server is running on http://localhost:${PORT}`); +}); +