-1

I have created an html form and I want to submit into the node.js server. while I submit the form it says "undefined" in the command promt.

Node JS:

    const express = require('express')
    const app = express()
    const path=require('path')
 
    app.get('/', function (req, res) {
       res.sendFile(path.join(__dirname,'index.html'))
})

    app.post('/submit',(req, res) => {
     console.log(req.body)
})

 
    app.listen(3000,function(){
      console.log("server started")
})

HTML Code:

 <form action="/submit" method="POST" id="form">
              <div class="text-white mt-2 mb-4">
                <svg xmlns="http://www.w3.org/2000/svg" width="23" height="23" fill="currentColor" class="bi bi-exclude mr-3" viewBox="0 0 16 16">
                  <path fill-rule="evenodd" d="M0 2a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v2h2a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2v-2H2a2 2 0 0 1-2-2V2zm12 2v7a1 1 0 0 1-1 1H4V5a1 1 0 0 1 1-1h7z"/>
                </svg><h2> Reddex </h2>
              </div>
                <div class="form-row"> 
                    <label for="inpname" class="text-white">Name     </label><br>
                    <input type="text" name="name" class="inp p-2 mb-2" placeholder="Name" id="inpname">
                </div>
                <div class="form-row">
                    <label for="inppassword" class="text-white">E-Mail</label><br>
                    <input type="email" name="Password" class="inp p-2 mt-2 mb-2" placeholder="Password" id="inppassword">
                </div>
                <div class="form-row">
                    <label for="inpname" class="text-white">Licence No </label><br>
                    <input type="password" name="Licence No" class="inpname p-2 mt-2 mb-2" placeholder="Licence No" id="inpname">
                </div>
                <div class="form-row">
                    <input type="submit" value="Submit" id="submit" class="bg-warning submit sub mt-4 p-2">
                </div>
            </form>
Reza sh
  • 787
  • 10
  • 20
  • 1
    You need to parse the body, which can be done using some for of body parser middleware such as `app.use(express.urlencoded({ extended: false }))` – Nick Parsons Dec 27 '20 at 08:53

1 Answers1

1

You do not need to install any npm packages for that manner. body-parser comes installed with latest Express versions. Add this line to the root of your Server file:

app.use(express.urlencoded({extended: true}))