Indie Heaven
Enumerating GET and POST Variables in Node.js


Adventures in Node.JS - Part 2

Porting from PHP

So here we are, we have a large mission critical business app written in PHP, and we need it to run in Node.js

How does this look, from a porting standpoint ?

One of the interesting things about PHP is the way it handles GET and POST variables. In PHP, the GET and POST argument are enumerated into arrays called $_GET and $_POST, and you can access them this way:

$somevar = $default_value;

if(isset($_POST['somevar']))
        if($_POST['somevar' != ""]
                $somevar = $_POST['somevar'];


This code will set the variable 'somevar' to a default value, and if there's anything in POST with the same name, it will override the default value. It's a pretty common piece of code on the input side of a screen.

Another nice thing about PHP is the "echo" statement. Whatever you echo, appears on the screen. For example, if you'd like to display a table it looks like this:

echo '<table>';
echo '<tr>';
echo '<td>';
echo '... some contents...;
echo '</td>';
echo '</tr>';
echo '</table>';

In Node, things are a little different. First, there is no convenient self-enumeration of GET and POST variables, we have to do it ourselves. And second, there is no convenient "echo" command, we have to build display strings and send them to the output using res.send() or a similar method.

One way around the display string issue, is to use a screen tool, either a template generate or something like React Hook Form that handles and maintains immutable variables. The simple way, is just to build the display string yourself. Express will handle any ordinary HTML, you can just build a string and send it to the output. The hard part is formatting a pretty page, the rest of it is easy.


Enumerating GET and POST variables


In Node, the GET and POST variables are in req.query and req.body, respectively. However the runtime will complain if you try to access these when they don't exist. So for instance, if you use this code

somevar = req.body.postvar

And postvar doesn't exist, Node will throw an exception. And, rather than handling exceptions at this low level, we would like a cleaner and friendlier way of doing this.

One way, is to enumerate the existing variables into PHP-compatible arrays. We can do it this way:

function enumerate_vars(req, _GET, _POST)
{
        for(var propName in req.query)
        {
                if(req.query.hasOwnProperty(propName))
                {
                        _GET[propName] = req.query[propName];
                }
        }

        for(var propName in req.body)
        {
                if(req.body.hasOwnProperty(propName))
                {
                        _POST[propName] = req.body[propName];
                }
        }
}


This function will enumerate all of req.query into _GET, and all of req.body into _POST, and the result is two arrays which you can read just like PHP. This way, the job of porting PHP code is reduced to removing dollar signs. Which can easily be done quickly and in an automated manner, using any of the popular Unix tools like sed (and awk is great too, very helpful).

In our application there were 2400 PHP files, most of which were user display screens. The pattern in most of them is, they read some GET and POST variables, retrieve something from the database, and display it on the screen. Typically the User ID (for example) would be passed in a POST variable, which is then picked up by the display screen and used to retrieve the user record from the database, this way the user name can be printed on a user-friendly screen.

Hopefully this has been a helpful tip, and later in this blog series we'll get into the heavy duty stuff like how we can organize monthly financial rollups. (If you know about these, they are "lengthy sequential transactions" that typically take minutes rather than seconds to execute, and even when these are run in mainframe batch environments the rest of the system is typically locked out to prevent midstream changes - the rollup transaction has to either succeed or fail in its entirety, because it updates financial records). We'll see how we can break up a lengthy batch process into asynchronous chunks while maintaining transaction integrity and handling any and all errors.


Back to the Console

Back to the Home Page


(c) 2024 Indie Heaven LLC
All Rights Reserved
webmaster@indieheaven.io