Chapter 11. Plain Text, JSON, and XML

By now, nearly all the important aspects of converting PHP code to Node.js code have been covered. This chapter covers one remaining topic of relatively minor importance: data formats.

To communicate, a client, such as a web browser, passes data to the PHP code or Node.js on the server as an HTTP GET or an HTTP POST and the PHP code or Node.js server passes data back to the client in an HTTP response. A client passes data either by submitting an HTML form or making an Ajax (Asynchronous JavaScript and XML) call. A server only has a single way to respond, though: by returning data in an HTTP response body.

If a client uses an HTTP GET to pass data, the data is sent as URL parameters, which on most web browsers are shown in the address bar after the question mark (?). If there is no question mark (?), there is no data sent; the fact that an HTTP GET was made to a certain URL is the only piece of data. If there is a question mark (?) and URL parameters, the parameters are in the “URL-encoded” format. For example, the following shows some URL-encoded data:

action=save&data=This%20is%20some%20data

URL-encoded data is name/value pairs separated by ampersands (&). Special characters, such as spaces ( ), are encoded using percent signs (%) followed by a specific two-digit hexadecimal number.

An HTTP GET request concatenates the URL-encoded data to the end of the URL in the first line in the HTTP header. The following shows an HTTP GET request that sends ...

Get Node.js for PHP Developers 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.