Express js body parser not working and req.body is undefined?
Asked Answered
D

2

11

I have the following in my node server using Express (truncated to the important parts):

var app = express.createServer();

app.all(/test/,function(req,res){
    console.log(req.headers);
    console.log(req.body);
    res.send("");
});

function appStart(cobrands){
    app.configure(function(){
        app.use(express.bodyParser());
        app.use(express.cookieParser());

        app.use('/min',express.static('../min'));
        app.use('/js',express.static('../js'));
        app.use('/css',express.static('../css'));
        app.use('/img',express.static('../img'));
    });
    app.listen(8080);
}

I then have a simple form that calls out to localhost:8080 like so:

<form action="http://localhost:8080/test" method="post">
    <input type="hidden" name="test" value="testing"/>
    <input type="submit" name="submit" value="to node"/>
</form>

But express.bodyParser doesn't seem to be doing anything, and req.body is undefined. Here's the output of the console.logs:

// req.headers
{ host: 'localhost:8080',
  'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) AppleWebKit/534.48.3 (KHTML, like Gecko) Version/5.1 Safari/534.48.3',
  'content-length': '27',
  accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  origin: 'file://',
  'content-type': 'application/x-www-form-urlencoded',
  'accept-language': 'en-us',
  'accept-encoding': 'gzip, deflate',
  cookie: '',
  connection: 'keep-alive' }
// req.body
undefined

Note: content-type is correctly defined as application/x-www-form-urlencoded as it should be for bodyParser to work, and I've verified that it's coming over by popping open the debug tools in Safari and verifying that the form data is present.

Debunk answered 17/9, 2011 at 1:58 Comment(1)
Looking at your code snippet you only had to use app.post('/search', ...) instead of app.get('/search', ...)Meier
S
20

http://expressjs.com/guide.html#configuration

Note the use of app.router, which can (optionally) be used to mount the application routes, otherwise the first call to app.get(), app.post(), etc will mount the routes.

What's happening is you're calling app.all() before adding any of the other middleware. Doing this effectively puts app.router in front of all of your other middleware, resulting in them never being used on requests that end inside your routes.

Mounting the application routes is pretty much the same as doing app.use(app.router);.

In the end your stack is looks this:

app.use(app.router); // Contains your /test/ route
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use('/min',express.static('../min'));
app.use('/js',express.static('../js'));
app.use('/css',express.static('../css'));
app.use('/img',express.static('../img'));

tl;dr Move your route between your call to app.configure() and app.listen().

Strader answered 17/9, 2011 at 3:29 Comment(4)
Hmm... I have everything setup like this, yet 'req.body' is still 'undefined'. Any idea what else can be wrong? Here's a code snippet: pastebin.com/DuawFmRrBurlesque
Vitaly, can you provide a snippet for how you're doing the request? req.body can be undefined if the Content-Type header isn't 'application/json' or 'application/x-www-form-urlencoded'.Strader
Never mind. I decided to use PHP with CodeIgniter instead and switch to Node.js only if need arises.Burlesque
"What's happening is you're calling app.all() before adding any of the other middleware" - this was exactly my problem. Thanks!Wrapper
C
2

I had similar problem and I figured out that this is becouse broken/missing express.bodyParser(). Instead express's bodyParser i've used connect's bodyParser and it worked:

app.use(require('connect').bodyParser());
Chemotherapy answered 26/9, 2011 at 11:37 Comment(3)
In my case, I was just setting the route before telling it to use bodyParser(), but thanks for the input - I hope someone is helped by this! Just a note - was this caused by a build of express? It seems like a rather large bug.Debunk
Didn't work work me. 'req.body' is still undefined. Could you tell which versions of node, connect and express you are using? Thanks.Burlesque
sorry, don't have this evirement already. It was experimental.Sarajane

© 2022 - 2024 — McMap. All rights reserved.