Handling POST Body Data

Another very common use for Express middleware is to handle body data inside a POST request. The data inside a request body can be in various formats, such as POST parameter strings, JSON strings, or raw data. Express’s body-parser middleware attempts to parse the data in the JSON data in the body of requests and properly format them as the req.body property of the Request object.

For example, if this middleware receives POST parameters or JSON data, it converts them to a JavaScript object and stores it as the req.body property of the Request object. Listing 19.4 illustrates using the body-parser middleware to support reading form data posted to the server.

The code in lines 5–11 handles the GET request and responds with ...

Get Node.js, MongoDB, and AngularJS Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.