Home > Mobile >  Express POST request values not received
Express POST request values not received

Time:11-15

I'm trying to make a simple registration page using Express but when I attempt to access the value of an input from the HTML form, I get undefined.

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}`);
})

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>

My file structure:

image of file structure

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