Sending JSON Responses

A growing trend has been to use JSON data to transfer information from a server to a client and then have the client dynamically populate the HTML elements on the page rather than have the server build HTML documents or parts of HTML documents and send the HTML to the client. Express facilitates sending JSON very nicely by providing the json() and jsonp() methods on the Response object. These methods use a syntax similar to that of send(), except that the body is a JSON stringify JavaScript object:

res.json(status, [object])res.json([body])res.jsonp(status, [object])res.jsonp([object])

The JavaScript object is converted to a JSON string and sent back to the client. In the case of jsonp(), the URL of the request object includes ...

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.