Getting body data from the client

To do this, we have to use the body-parser module. As I mentioned previously, body-parser is going to take your JSON and convert it into an object, attaching it onto this request object. We're going to configure the middleware using app.use. The app.use takes the middleware. If we're writing custom middleware, it'll be a function; if we're using third-party middleware, we usually just access something off of the library. In this case, it's going to be bodyParser.json getting called as a function. The return value from this JSON method is a function, and that is the middleware that we need to give to Express:

var app = express();app.use(bodyParser.json());

With this in place, we can now send JSON to our Express ...

Get Advanced Node.js 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.