Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
How to use child process and spawn in React.js webapp

I am making this web app about bookstore which recommends books based on book user selects. I want it to run a python script and for that, I have used child process looking at this thread How to call a Python function from Node.js

Here's the code:

const spawn = require("child_process").spawn;
const pythonProcess = spawn('python',["../../../python/book_recommendation.py","Timeline"]);

const Recommendations = pythonProcess.stdout.on('data', (data) => {
    console.log(data);
});

module.exports = Recommendations;

I want this code to run when I click on the product and get directed to productScreen page. But I get the error "TypeError: Spawn is not a function". With some google searching, I found out that I m calling this function through react and not node.js. I m using react as frontend and node.js as backend. How can I solve this problem?

Here's how I imported this in react ProductScreen.js page

import { Recommendations } from "../machine-learning/recommendation_engine"

Here's the file structure of my project

enter image description here

Update:

const spawn = require("child_process").spawn;
const pythonProcess = spawn('python',["../python/book_recommendation.py","Timeline"]);
app.get("/product/:id", (req, res) => {
  pythonProcess.stdout.on('data', (data) => {
      console.log(data);
  });
});

I moved the above code in server.js backend folder now. How do I call it?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I don't think it's good architecture wise. But if you insist on doing it that way, then you need to spawn the child process when the requests hit. You could then pipe its output to the response stream. In this example I am using pipeline which is somewhat safer than mystream.pipe.

const spawn = require("child_process").spawn;
const { pipeline } = require('stream');

app.get("/python-process", (request, response) => {
    const pythonProcess = spawn('python', [
        "../python/book_recommendation.py",
        "Timeline"
    ]);
    // pipe the python process stream into the response stream
    pipeline(pythonProcess, response, (err) => err && console.warn(err));
});

Note, I haven't tested this, but it is the gist of how you could go about it.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda