O'Reilly logo

JavaScript JSON Cookbook by Ray Rischpater

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Accepting JSON using Node.js

Different web server systems accept data posted by a client in different ways. That being said, in most cases, you read the data piecewise as it comes in from the client and once the POST request finishes, process it as a batch. Here's how to do it with Node.js.

How to do it...

In our case, we accept JSON submitted from the client via HTTP POST requests. To do this, we need to read the data from the client, aggregate it in a string, and when all of the data arrives at the server, convert the data from a JSON string to a JavaScript object. In json-encoder, js, we modify it to read as the following:

 // … beginning of script is the same as in the introduction
    if (req.method == 'POST') {
      console.log('POST');
 var body = ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required