Home > Back-end >  trying to console.log(req,body.name) in express app but it shows undefined
trying to console.log(req,body.name) in express app but it shows undefined

Time:11-14

I'm trying to make a simple registration page using express but when I am consol.log the name in post request then it shows undefined, but I included

var bodyParser = require('body-parser')
app.use( bodyParser.json() );       
app.use(bodyParser.urlencoded({
  extended: true
})); 

but it's still not working. Please help me to resolve this issue Thank You.

in app.js

var express = require('express');
var path = require('path');
var app = express();
const handlebars = require('express-handlebars');

const { json } = require('express');
var port = process.env.PORT || 5000;


var bodyParser = require('body-parser')
app.use( bodyParser.json() );       
app.use(bodyParser.urlencoded({
  extended: true
})); 

app.use(express.json());
app.use(express.urlencoded({ extended: false }));


app.use(express.static(path.join(__dirname, '/public')))
app.set('views', path.join(__dirname, "views"));
app.set('view engine', 'hbs');


app.get('/', (req,res)=>{
    res.render('index');
})
app.get('/register', (req,res)=>{
    res.render('register');
})

app.post('/register',(req,res)=>{
    const name = req.body.name;
    console.log(req.body.name);
    res.render('index');
})

app.listen(port, ()=>{
    console.log(`Server is running at port ${port}`);
})

in register.hbs

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/stylesheets/style.css">
    <title>Registration Website</title>
</head>
<body>
    <h1>Welcome to our Registration page</h1>
    <div class="container">
        <div class="form-box">
            <form action="/register" method="post"  enctype="multipart/form-data">
                <input type="text" name="name" placeholder="Enter Your name" autocomplete="off">
                <input type="email" name="email" placeholder="Enter Your email" autocomplete="off">
                <input type="password" name="password" placeholder="Enter password" autocomplete="off">
                <input type="password" name="cpassword" placeholder="Confirm your password" autocomplete="off">

                <button type="submit" name="submit">Register</button>
            </form>
        </div>
    </div>
</body>
</html>

in pakage.json

{
  "name": "practice3",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "express-handlebars": "^6.0.1",
    "hbs": "^4.1.2",
    "mongodb": "^4.1.4",
    "mongoose": "^6.0.12"
  }
}

file structuring enter image description here

CodePudding user response:

Your <form> element is set to send POST requests encoded with multipart/form-data, therefore the payload received with Express cannot simply fetch the values from the req.body element. The multipart/form-data encoding is used for encoding and sending files, so in a normal form such as this, it is unrequired.

By default, if the enctype property is not provided, application/x-www-form-urlencoded is used and this allows your POST payload to be accessed through the request body in Express.

<form action="/register" method="POST">
    <input type="text" name="name">
    <button type="submit">Submit</button>
</form>
app.post("/register", (req, res) => {
    const name = req.body.name; // Fetches the 'name' input's value.
    ...
});
  • Related