Search code examples
node.jsibm-cloudiot

Sample node.js doing form handling and processing - in Node.js


I need a pointer or sample tutorial or some examples to form handling in Node.js. I have a form.html which has 3 buttons "Forward", "Backward", "Stop" and they will have to call a process get and appropriately send messages to the backend component to take appropriate action. My sample node.js that comes when I create the application is good, but it does not have form processing, so a link to some worked examples would help a lot.


Solution

  • You can use the express framework to process the form.

    <form action="/processForm" method="POST">
      What's your name?: <input type="text" name="yourname"><br>
      <input type="submit" value="Forward">
    </form>
    

    node backend:

    app.post('/processForm',function(req,res){
      var yourname=req.body.yourname;
      res.end("Hello " + yourname);
    });
    

    Great tutorials: https://codeforgeek.com/2014/09/handle-get-post-request-express-4/ http://www.hacksparrow.com/form-handling-processing-in-express-js.html