86 | Chapter 4: Advanced Callbacks 86 | Chapter 4: Advanced Callbacks

86 | Chapter 4: Advanced Callbacks 86 | Chapter 4: Advanced Callbacks

function lfo_create ( $obj , $name ) { return array ( 'obj' => $obj , 'func' => $name );

function lco_create ( $obj , $name , $args ) {

return array ( 'obj' => $obj , 'func' => $name , 'args' => $args ); }

function lc_call ( $lc ) { if ( $lc [ 'func' ] == 'anon_noop' ) {

$lc [ 'obj' ] -> lf_anon_noop ( $lc );

} else if ( $lc [ 'func' ] == 'anon_writefile' ) {

$lc [ 'obj' ] -> lf_anon_writefile ( $lc ); } return $lc ; }

These PHP 4 functions can be converted to Node.js:

function lfo_create ( obj , name ) { return { 'obj' : obj , 'func' : name };

function lco_create ( obj , name , args ) { return { 'obj' : obj , 'func' : name , 'args' : args };

function lc_call ( lc ) { if ( lc [ 'func' ] == 'anon_noop' ) {

lc [ 'obj' ]. lf_anon_noop ( $lc );

} else if ( lc [ 'func' ] == 'anon_writefile' ) {

lc [ 'obj' ]. lf_anon_writefile ( lc ); } return lc ; }

Chapter 8 shows how to convert PHP classes to Node.js classes. The implementation and usefulness of adding simulated anonymous functions to classes will become more apparent in that chapter.

In the next chapter, the differences between sending an HTTP response in PHP and in Node.js will be addressed. As might be expected, HTTP responses in PHP are sent using

a blocking technique whereas HTTP responses in Node.js are sent using a nonblocking technique. The linearity-based conversion recipes from Chapter 3 and the simulation- based conversion recipes from this chapter will be the foundation for refactoring PHP HTTP response code to be easily converted to Node.js.

PHP 4 | 87

CHAPTER 5

HTTP Responses

In previous chapters, a development environment suitable for PHP to Node.js conver‐ sion was set up, a Node.js framework for hosting Node.js code converted from PHP pages was written, and the original PHP code was refactored to make it friendlier to PHP to Node.js conversion (in particular, in terms of handling callbacks). With that done, we are ready to copy and paste the PHP code from the .php file into the corre‐ sponding .njs file.

At this point, some or all of your .php files will have a corresponding .njs file in the same directory. All the .njs files in your project will have a stub implementation of the page similar to the following Node.js code:

var initreq = require ( './initreq.njs' );

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 () {

function page ( req , res , pre , cb ) { res . writeHead ( 200 , { 'Content-Type' : 'text/plain' });

res . end ( 'index.njs' ); cb (); }

The page() function has a stub implementation. We will copy and paste the PHP code from the .php file into the implementation of the page() function soon. But first, let’s examine how HTTP responses are returned in both PHP and Node.js.

An HTTP response is similar to an HTTP request: it can contain multiple HTTP re‐ sponse headers and a single HTTP response body. In actual implementation, the HTTP response headers are separated from the HTTP response body by a single blank line. The stub implementation of the page() function will produce an HTTP response similar to this:

HTTP/1.1 200 OK Content-Type: text/plain

index.njs

In the first header, the one containing HTTP/1.0 200 OK, the 200 is the important part. 200 is an HTTP status code that means that the HTTP request succeeded completely normally. The OK after 200 is just a short description of the status code.

In the second header, Content-Type says that this is a plain text web page, instead of an HTML web page. If it was an HTML web page, the second header would set text/ html as the content type instead of text/plain.

The blank line between Content-Type: text/plain and index.njs separates the HTTP response header for the HTTP response body.

Finally, the index.njs line is the HTTP response body. If the user goes to a particular PHP page or clicks a hyperlink, the HTTP response body would contain a full page of HTML. If a browser is running JavaScript and that JavaScript makes an AJAX call, the HTTP response body might contain plain text or data in the JSON format.