94 | Chapter 5: HTTP Responses 94 | Chapter 5: HTTP Responses

94 | Chapter 5: HTTP Responses 94 | Chapter 5: HTTP Responses

In Chapter 3 , the concept of linearity was described. It was largely about keeping a single code path through the PHP page, but when a code path needed to be split, keeping both subsequent code paths independent and not allowing them to recombine. The end() and cb() function calls will be at the end of some of these code paths, although they will not be at the end of some others. However, they will not ever need to be put in the middle of a code path or somewhere else in the code. Logically, making the HTTP response complete by calling the end() function and doing post-processing on the page by calling the cb() function can only happen at the end of a code path. By knowing and understanding the code paths generated by the linearity concept in Chapter 3 , you have

a finite set of locations to consider putting the end() and cb() function calls.

A helpful technique can be to add a “do nothing” function call to the PHP codebase to remind yourself where the end() and cb() function calls will need to be added when the code is converted to Node.js. The following PHP code shows an eocp() function, which stands for “end of code path”; it will only print out the data argument when invoked on the PHP side, but can serve as a marker to indicate that the Node.js code will need to make its end() and cb() function calls at this location:

$res = '' ; $cb = '' ;

function eocp ( $res , $cb , $data ) {

print $data ; } …

eocp ( $res , $cb , '' );

In fact, the eocp() function can be converted and implemented in Node.js as a quick- and-dirty way to call the end() and cb() functions in the correct places:

function eocp ( res , cb , data ) {

res . end ( data ); cb (); } …

eocp ( res , cb , '' );

When the PHP and Node.js codebases are compared using a visual diff feature or pro‐ gram as described in Chapter 1 , the eocp() function calls in both codebases should be matched and able to be visually verified to be in all the correct places.

Body | 95

The eocp() function can work and is a good approach but there is an alternative. An alternative approach is to remove all the end() function calls from the page()

function and put them into the callback function, the cb callback variable, that is passed from the exports.serve() function. The following code shows the exports.serve() function with the end() function call:

exports . serve = function ( req , res ) {

var pre = {};

initGET ( req , pre , function () { initPOST ( req , pre , function () { initCOOKIE ( req , pre , function () { initREQUEST ( req , pre , function () { page ( req , res , pre , function () {

res . end (); }); }); }); }); }); };

With this approach, the eocp() function and the end() function call isn’t needed in the page() function implementation. Instead, an empty cb() function can be used in PHP

to serve as a marker:

function cb () { } …

cb ();

In Node.js, the cb() function is already defined, so a new function is not needed. The cb() function is called in the same place that it would normally be called. The end() function call is removed since it is now done inside the exports.serve() function: