Express bodyParser req.body undefined

1,123 views
Skip to first unread message

David R. Lee

unread,
Jul 8, 2013, 12:00:19 AM7/8/13
to expre...@googlegroups.com
I'm having a hard time figuring out why I can't get req.body to be defined.  Could someone help me see the problem?
Basically using jQuery plugin ajaxForm to submit a form.  I tried express 3.3 and 3.0.6


Server config

server.configure(function () {
    server.use(express["static"](__dirname + "/public"));

    server.use(express.errorHandler({
        dumpExceptions:true,
        showStack:true

    }));

    server.use(express.bodyParser());
    server.use(server.router);
});


HTTP Request

  1. Request URL:
  2. Request Method:
    POST
  3. Status Code:
    500 Internal Server Error
  4. Request Headersview source
    1. Accept:
      */*
    2. Accept-Encoding:
      gzip,deflate,sdch
    3. Accept-Language:
      en-US,en;q=0.8
    4. Cache-Control:
      no-cache
    5. Connection:
      keep-alive
    6. Content-Length:
      336
    7. Content-Type:
      application/x-www-form-urlencoded
    8. Cookie:
      connect.sid=s%3Ae0oxS0kGlARN0RVb2kpTWW4B.j6Kc5oBVjVc0LWBHxlFRJQ2Lm9l06j9xYA6pUJyzk9Q
    9. Host:
      localhost:8001
    10. Origin:
    11. Pragma:
      no-cache
    12. Referer:
    13. User-Agent:
      Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/28.0.1500.52 Chrome/28.0.1500.52 Safari/537.36
    14. X-Requested-With:
      XMLHttpRequest
  5. Form Dataview sourceview URL encoded
    1. name:
      asdf
    2. organization:
      asdf
    3. email:
      asdf@adf
    4. phone:
      asdfas
    5. purpose:
      new-service
    6. inquiry:
      asdfasdf
    7. recaptcha_challenge_field:
      03AHJ_Vut8YpaUDKN5cJLqCXnMAZJLRgZpElQTg7LnynXQP_mZdiSQSwaH96q2dnUfvxAx2CVXZuU1iouZJeEMvSJQxHhB612T088gqCdhMs-ag2psk6qplGyjFjoIEiCr3g_ZHQyLTWQ2HGoUeMCKL9ixnPvmrk15jC4v4JVT8vTeKK_TGP3FarU
    8. recaptcha_response_field:
      asdf
  6. Response Headersview source
    1. Connection:
      keep-alive
    2. Content-Type:
      text/plain
    3. Date:
      Mon, 08 Jul 2013 03:53:43 GMT
    4. Transfer-Encoding:
      chunked
    5. X-Powered-By:
      Express

Route
  app.post('/rest/email', recaptchaController, function(req, res) {


Error occurs in recaptchaController
module.exports = function(Recaptcha) { return function(req, res, next) { var data = { remoteip: req.connection.remoteAddress, challenge: req.body.recaptcha_challenge_field, response: req.body.recaptcha_response_field };


req.body is undefined

David Dripps

unread,
Jul 11, 2013, 12:41:32 PM7/11/13
to expre...@googlegroups.com
You've defined recaptchaController as a function that returns the route. Right now it's assuming function(Recaptcha) is the route, not the returned function. Change your route to this:

app.post('/rest/email', recaptchaController(), function(req, res) {

David R. Lee

unread,
Jul 11, 2013, 8:07:38 PM7/11/13
to expre...@googlegroups.com
Hey David thank you so much for responding.
I missed some parts of the code that might explain my code better (sorry).  I have a similar logic working in another app, and when I modified it a bit for this app, I'm getting lost.


module.exports = function (app, Recaptcha, transport) {
  var recaptchaController = require('../controllers/recaptchaController')(Recaptcha);

  app.post('/rest/email', recaptchaController, function(req, res) {



I have it defined like that so it should be returning the function with (req, res, next) as the parameter..
I just started trying to use the node debugger, but it does get into the function properly
I put a debug point in bodyParser, but it's not getting in there (might be a user error)

I end up getting the req object with no body

David R. Lee

unread,
Jul 11, 2013, 9:27:16 PM7/11/13
to expre...@googlegroups.com
Found my mistake - my route definition was above the server configuration.  For some reason, I didn't think this would affect the behavior.


require('./server/config/routes')(server, Recaptcha, transport); // I moved this line to below server.configure

server.configure(function () {
server.use(express.logger()); 
server.use(express.favicon());
    server.use(express["static"](__dirname + "/public"));

    server.use(express.bodyParser());
    server.use(express.methodOverride());
    server.use(server.router);
});

David Dripps

unread,
Jul 12, 2013, 1:55:55 AM7/12/13
to expre...@googlegroups.com, expre...@googlegroups.com
Yep. That'll do it. I've wrestled with the order of my middleware more times than I care to admit :)

Glad you figured it out!
--
You received this message because you are subscribed to a topic in the Google Groups "Express" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/express-js/bu7wwQA0Xg8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to express-js+...@googlegroups.com.
To post to this group, send email to expre...@googlegroups.com.
Visit this group at http://groups.google.com/group/express-js.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply all
Reply to author
Forward
0 new messages