Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reading html form input with with node.js and express.js

I am trying to read the input from an HTML form and use it in Node.js.

This is my HTML form:

    <form action="/myform" method="POST">
        <input type="text" name="mytext" required />
        <input type="submit" value="Submit" />
    </form>

This is my Node.js code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


app.post('/myform', function(req, res){ 
  console.log(req.body.mytext); //mytext is the name of your input box
});

I am getting this error:

TypeError: Cannot read properties of undefined (reading 'mytext')
   

Please help me out.

>Solution :

Make sure you are parsing the request. For that, add the line below at the top of your server root file:

app.use(express.urlencoded({
  extended: true
}));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading