0

I can't access POST data in my node.js app. I'm also using express.

app.js:

app.post("/sent_message", function(request, response){
    console.log(request.body.message.name + " " + request.body.message.content);
});

HTML:

<form method="post" action="/sent_message">
            <input type="text" name="message[name]">
            <textarea name="message[content]"></textarea>
            <input type="submit">
</form>
blockaj
  • 323
  • 3
  • 13

1 Answers1

1

You must include app.use(express.bodyParser());.

blockaj
  • 323
  • 3
  • 13
  • `bodyParser` is not recommended. Use `express.urlencoded()` and `express.json()` instead. If you need to parse multipart form data (i.e. file uploads), you should use Busboy or Formidable. – Ethan Brown Feb 14 '14 at 03:51
  • @EthanBrown, and what does JSON or URL encodings have to do with a POST request? – Thank you Feb 14 '14 at 09:00
  • A POST request transmits the fields (normally) as the body of the request. That body is encoded in a format called "URL encoding", and that's what the `express.urlencoded()` middleware does. (It's actually Connect middleware, but it's available through Express). `express.json()` is not so commonly used, but some clients (especially AJAX requests) encode the body in JSON format instead of URL encoded. – Ethan Brown Feb 14 '14 at 09:03
  • `express.bodyParser()` simply includes `express.urlencoded()`, `express.json()`, and `express.multipart()`. The reason it's not recommended is that `express.multipart()` is deprecated and will be removed entirely in the next version of Express. Fortunately, Busboy and Formidable handle multipart encoding quite adroitly. – Ethan Brown Feb 14 '14 at 09:05