Rares Lungescu
Rares Lungescu

Reputation: 33

Cannot access req.body, even when using body-parser

I am trying to get my login functionality up and running, but i am unable to access the req.body object.

When i first encountered the issue, the post route was not even getting triggered (console.log was not showing at all in terminal) and the request would eventually time out. This stopped after i added this line in the body-parser initialization:

 type: 'x-www-form-urlencoded',

Now, the console.log form the route appears in the terminal, but both parameters are empty.

Router.js

const express = require('express'),
    router = express.Router();

router.post('/signup', function (req, res) {
    console.log(req.body.name, req.body.password);
    res.send('posted');
});

module.exports = router;

app.js

const express = require('express'),
    app = express(),
    bodyParser = require('body-parser'),
    cookieParser = require('cookie-parser'),
    session = require('express-session'),
    http_port = process.env.HTTP_PORT;

const http = require('http').createServer(app);
app.use(express.static(path.join(__dirname, 'public')));
app.use(
    express.urlencoded({
        type: 'x-www-form-urlencoded',
        extended: true,
    })
);
app.use(express.json());
app.use(cookieParser());
app.use(require('./backend/router'));
http.listen(process.env.PORT || http_port, () =>
    console.log(`App listening on port ${http_port}`)
);

Form from index.html

                      <div>
                        <form method="POST" enctype="application/x-www-form-urlencoded">
                            <div class="row">
                                <label>Full Name</label>
                                <input
                                    name="name"
                                    type="text"
                                    required
                                    placeholder="Enter your name"
                                />
                            </div>
                            <div class="row">
                                <label>Email</label>
                                <input
                                    name="email"
                                    type="email"
                                    required
                                    placeholder="Enter your email"
                                />
                            </div>
                            <div class="row">
                                <label>Password</label>
                                <input
                                    name="password"
                                    type="password"
                                    required
                                    placeholder="Enter your password"
                                />
                            </div>
                            <div class="row">
                                <label>Confirm Password</label>
                                <input
                                    name="password_confirm"
                                    type="password"
                                    required
                                    placeholder="Confirm your password"
                                />
                            </div>
                            <div id="button" class="row">
                                <button
                                    formmethod="POST"
                                    formenctype="application/x-www-form-urlencoded"
                                    type="submit"
                                >
                                    Sign Up
                                </button>
                            </div>
                        </form>
                    </div>

Upvotes: 1

Views: 399

Answers (3)

Rares Lungescu
Rares Lungescu

Reputation: 33

I was not able to fix it, but i managed to find a workaroud. The info from the form is also in request.fields, so i am going to use that instead of request.body. I still would like to know why it doesn't work as normal though.

Upvotes: 1

Will
Will

Reputation: 105

Perhaps try app.use(bodyParser.json()); instead of app.use(express.json());

Upvotes: 0

max
max

Reputation: 310

Remove type: 'x-www-form-urlencoded' in your parser and add action="http://localhost:3000/signup" to your form

Upvotes: 0

Related Questions