Implementing POST Servers

Implementing a POST server is similar to implementing a GET server. In fact, you may end up implementing them together in the same code for the sake of convenience. POST servers are handy if you need to send data to the server to be updated, as with form submissions. To serve a POST request, you need to implement code in the request handler that reads the contents of the post body out and processes the contents.

Once you have processed the data, you dynamically populate the data you want to send back to the client, write it out to the response, and then call end() to finalize the response and flush the Writable stream. Just as with a dynamic GET server, the output of a POST request may be a webpage, an HTTP snippet, JSON data, or some other data.

Listing 7.5 shows the basic implementation of a dynamic web service handling POST requests. In this case, the web service accepts from the client a JSON string representing an object that has name and occupation properties. The code in lines 4–6 reads the data from the request stream, and then in the event handler in lines 7–14, the data is converted to an object and used to build a new object with message and question properties. Then in lines 14 to 17 the response JSON string is converted to an object and displayed on the console.

Listing 7.5 http_server_post.js: Implementing a basic HTTP server that handles HTTP POST requests


01 var http = require('http'),
02 http.createServer(function (req, res) {
03   var jsonData = "";
04   req.on('data', function (chunk) {
05     jsonData += chunk;
06   });
07   req.on('end', function () {
08     var reqObj = JSON.parse(jsonData);
09     var resObj = {
10       message: "Hello " + reqObj.name,
11       question: "Are you a good " + reqObj.occupation + "?"
12     };
13     res.writeHead(200);
14     res.end(JSON.stringify(resObj));
15   });
16 }).listen(8080);
17
18
19 var http = require('http'),
20 var options = {
21   host: '127.0.0.1',
22   path: '/',
23   port: '8080',
24   method: 'POST'
25 };
26 function readJSONResponse(response) {
27   var responseData = '';
28   response.on('data', function (chunk) {
29     responseData += chunk;
30   });
31   response.on('end', function () {
32     var dataObj = JSON.parse(responseData);
33     console.log("Raw Response: " +responseData);
34     console.log("Message: " + dataObj.message);
35     console.log("Question: " + dataObj.question);
36   });
37 }
38 var req = http.request(options, readJSONResponse);
39 req.write('{"name":"Bilbo", "occupation":"Burglar"}'),
40 req.end();


Listing 7.6 shows a basic implementation of an HTTP client that sends JSON data to the server as part of a POST request. The request starts in line 20. Then in line 21 a JSON string is written to the request stream, and line 22 finishes the request with end().

When the server sends back the response, the on('data') handler in lines 10–12 reads the JSON response. Then the on('end') handler in lines 13–18 parses the response into a JSON object and outputs the raw response, message, and question. Figure 7.4 shows the output of the HTTP POST client.

Listing 7.6 http_client_post.js: A basic HTTP client that sends JSON data to the server, using POST, and that handles the JSON response


01 var http = require('http'),
02 var options = {
03   host: '127.0.0.1',
04   path: '/',
05   port: '8080',
06   method: 'POST'
07 };
08 function readJSONResponse(response) {
09   var responseData = '';
10   response.on('data', function (chunk) {
11     responseData += chunk;
12   });
13   response.on('end', function () {
14     var dataObj = JSON.parse(responseData);
15     console.log("Raw Response: " +responseData);
16     console.log("Message: " + dataObj.message);
17     console.log("Question: " + dataObj.question);
18   });
19 }
20 var req = http.request(options, readJSONResponse);
21 req.write('{"name":"Bilbo", "occupation":"Burglar"}'),
22 req.end();


Image

Figure 7.4 Implementing an HTTP POST server serving JSON data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset