Skip to content Skip to sidebar Skip to footer

Client-side Calls Server To Run Python

I'm quite a beginner and I've been looking for an answer for days now.. I am building a webapp using nodejs. Here is the app.js I node to start (my server-side) http = require('htt

Solution 1:

You're right about your errors

require is defined on server side only. funcs.js is executed on client side, so you can't use require.

And you'll need to make an AJAX call in order to reach your goal.

// server.jsconst { exec } = require('child_process');

const http = require('http');

const server = http.createServer((req, res) => {
  /* When a request will call the http://localhost:5000/run url
   it will execute this code block */if (req.url === '/run') {
    exec('python ...', () => {
      // ...

      res.write('Python script executed');
      res.end();
    });
  }
});

server.listen(5000);
// funcs.jsfunctionturnOff() {
    console.log("enter Off");

    // Turn off will send a request on the http://localhost:5000/run endpointfetch("/run", {
      method: "POST"
    })
    .then(() => {
       document.getElementById("fil").innerHTML = "running python...";
    })
    .catch(() => {
       document.getElementById("fil").innerHTML = "error..."+err;
    })
}   

Post a Comment for "Client-side Calls Server To Run Python"