Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs- Req.body undefined in post with express 4.x

I am using a middleware body-parser to encoded the form values to get in req.body object. But as I debug my code, found out req.body is undefined. Here is my code

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

Listen Post request

app.post('/newCategory', function (req,res) {

            //express attached the form encoded values into body
            var categoryName = req.body.categoryName;
        });

Html Form

<form action="/newCategory" role="form" method="post" class="form-inline">
    <input type="text" name="categoryName" placeholder="Category name" class="form-control" />
    <input type="submit" value="New Category" class="btn btn-primary" />
</form>
like image 482
Ammar Khan Avatar asked Jul 15 '14 10:07

Ammar Khan


4 Answers

I noticed that the order is very important. Usually the router should be declared in the end before starting the server . Eg: 1.i import the required files

var express            = require("express");
var bodyParser         = require("body-parser");
var morgan             = require("morgan");
var db              = require("./db.js");

var app                = express();

2.i declare the other stuffs

app.set("port", process.env.PORT || 3000);

//app.use(express.static(__dirname + '/public'));
app.use(morgan('dev') ); // Log every request to console
app.use(bodyParser.urlencoded({
    extended: true
}));

app.use(bodyParser.json());

3. AFTER I INCLUDE ROUTES - most important step

var routes = require('./routes/routes');
routes(app);   //routes shall use Express
  1. start the server

    app.listen(app.get("port"), function () { console.log("Express server listening on port " + app.get("port")); });

And then will work .. i'm not sure why but after it happen few time i learn the lesson .

like image 115
Teodor Avatar answered Oct 18 '22 03:10

Teodor


This solved the problem for me

var bodyParser = require('body-parser');
var app=express();
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());

Hope this help

like image 31
ackuser Avatar answered Oct 18 '22 03:10

ackuser


Just ran into the same issue. It looks like I resolved my problem by moving my code to map routes after the urlencoded line. I am now seeing req.body in the post.

app.use(bodyParser.urlencoded({ extended: true }));


// Map routes
var controllers = require("./controllers");
controllers.init(app);
like image 12
user3926346 Avatar answered Oct 18 '22 03:10

user3926346


If you're using urlencoded with { extended:false }, req.body will return the unparsed raw string from the form categoryName=test. Meaning req.body.categoryName will be undefined.

Try passing true so it can parse the form data using the qs module.

app.use(bodyParser.urlencoded({
    extended: true
}));
like image 2
Ben Fortune Avatar answered Oct 18 '22 03:10

Ben Fortune