1

I am trying to make a communication between NodeJS and Python by making a post request from file .ejs rendered by NodeJS. But I don't know how to listen to that post request with python. I tried flask as this link How to send and receive HTTP POST requests in Python but flask and node cannot run on the same port.

In index.ejs:

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body>
    <div>
      <form action="/new"  method="POST">
         <p>Calibration PH (for example: 4) : </p>
         <input type="text" name="PH">
         <input type="submit" value="Submit">
      </form>
    </div>
  </body>
</html>

In app.js:

app.set('view engine', 'ejs');
app.get('/',(req,res)=>{
   console.log("OK")
   res.render('index');
});
app.post('/new',(req,res)=>{
   var number = req.body.PH;
   res.redirect('/');
   console.log(number);

})
app.listen(process.env.PORT || 3000, process.env.IP, function(){
   console.log("SERVER IS RUNNING!");
})
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • you need to run it on separate ports, and work with `http requests` – Druta Ruslan Jun 11 '18 at 19:56
  • Set them up on different ports. – Norrius Jun 11 '18 at 19:56
  • Norrius , I tried @app.route('localhost:3000/new', methods=['POST']) But it said url has to start with "/". How can I listen to localhost:3000/new when flask run in port 5000 – Anh Tú Mai Jun 11 '18 at 19:59
  • @AnhTúMai You do not have to use flask, this creates a server, but requests that a client creates. A client does not occupy a port. – eyllanesc Jun 11 '18 at 20:02
  • @eyllanesc I understand, are there any libraries, or ways that I can receive post request in python? – Anh Tú Mai Jun 11 '18 at 20:08
  • “Receiving a post request” is done with an HTTP server, and Flask is a good choice. – Norrius Jun 11 '18 at 20:50
  • @Norrius , can you show me the code, I tried the whole evening and cannot find the right way to make Flask listen to 'localhost:3000/new' when it is running in port 5000 by default. Otherwise if I set port of my nodeJS to 5000 , they cannot run at the same time – Anh Tú Mai Jun 11 '18 at 20:55
  • Please see [this question](https://stackoverflow.com/questions/41940663/why-cant-i-change-the-host-and-port-that-my-flask-app-runs-on) on how to specify the port in `app.run()` *or* in `flask` CLI tool (depending on which of those you use). – Norrius Jun 11 '18 at 21:13
  • @AnhTúMai requests is a library: http://docs.python-requests.org/en/master/ – eyllanesc Jun 11 '18 at 21:15
  • @AnhTúMai Do you want to receive post ?, according to what I see you have a server written with node.js, and that is what I would receive. Am I correct? What I understand is that you want to send a post to that server. Correct me if necessary – eyllanesc Jun 11 '18 at 21:24
  • @eyllanesc yes, you are right. NodeJS and .ejs is to create a human interface so that users can input a number. And I need that number to be in python file, for some reasons, not NodeJs, which I can easily do with express. So I want Python to receive post request, not send a post to the server. – Anh Tú Mai Jun 12 '18 at 01:53
  • @AnhTúMai mmm, that is not possible directly, the server does not send data to the client if it does not ask for it, I recommend you check the client-server architecture. A possible solution is that you store that value in the cache with js, and the client makes an order every certain interval of time asking for a new data, and if there is, then they give it the value of the cache. – eyllanesc Jun 12 '18 at 02:02

0 Answers0