Handling POST data
One of the most common REST
methods used in network applications is POST. According to the REST
specification, a POST
is not idempotent, as opposed to most of the other well-known methods (GET
, PUT
, DELETE
, and so on) that are. This is mentioned in order to point out that the handling of POST
data will very often have a consequential effect on an application's state, and should therefore be handled with care.
We will now discuss the handling of the most common type of POST
data, that which is submitted via forms. The more complex type of POST
—multipart uploads—will be discussed in Chapter 4, Using Node to Access the Filesystem.
Let's create a server which will return a form to clients, and echo back any data that client submits with that form. We will need to first check the request URL
, determining if this is a form request or a form submission, returning HTML
for a form in the first case, and parsing submitted data in the second:
const http = require('http'); const qs ...