Forums >> Programming >> Open Source >>
Making My Own Example (That Does a POST)




Posted:
bvstone

Making My Own Example (That Does a POST)

 
Making My Own Example (That Does a POST)

Now that we had the sample provided by IBM working, I decided I wanted to test out my own application that I had running on my linux machine.

This process was created to duplicate our key generation software already written in RPG.  I figured it might be a good idea to have a backup on another box to be able to generate keys if I needed to.  So a few months ago I wrote a package that did just that.

First I created my HTML page.  It looks like the following:

Very simply, it accepts some data to generate the key.  The HTML looks like the following:

<!DOCTYPE HTML> 
<html lang="en-US">
	<head> 
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>BVSTools Key Generation</title>
	</head> 
<style> 
input {
	height:30px;
	border:#ccc solid 1px 
}
input[type="text"] {
	width:500px 
}
input[type="submit"] {
	margin:1em; 
	width:120px 
} 
</style>
<body> 
	<form name="input" action="query" method="post">
		<div>Software</div>
		<input type="text" name="software"/>
		<div>Logical Serial Number</div>
		<input type="text" name="lsn"/>
		<div>Model</div>
		<input type="text" name="model"/>
		<div>Days</div>
		<input type="number" name="days"/>
		<div>Month</div>
		<input type="number" name="month"/>
		<div>Day</div>
		<input type="number" name="day"/>
		<div>Year</div>
		<input type="number" name="year"/>
		<input type="submit" value="Query"/>
	</form> 
</body>
</html>

Next we created the node.js source.  This source would only accept POST as the request type, which we will see in the following code:

var http = require('http');
var qs = require('querystring');
var fs = require('fs');
var url = require('url');
var keys = require('software');

var DBname = "*LOCAL"; 
var ip = "192.168.1.39";
var port = 80;

function padding_right(s, c, n) {
  if (! s || ! c || s.length >= n) {
    return s;
  }
  var max = (n - s.length)/c.length;
  for (var i = 0; i < max; i++) {
    s += c;
  }
  return s;
}

var webserver = http.createServer(function (req, res) { 
	var realPath = __dirname + url.parse(req.url).pathname;
	fs.exists(realPath,function(exists){
		if(!exists){
			
			if (req.method == 'POST') {
				var body = '';
				
				req.on('data', function(data) {
					body += data;
		            // Too much POST data, kill the connection!
					// 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
					if (body.length > 1e6)
						req.connection.destroy();
				});
				
				req.on('end', function() {
					res.writeHead(200, {'Content-Type': 'text/plain'}); 
					var post = qs.parse(body);
					var results = genKeys(post);
					res.write(results[0] + ':' + results[1]);
					res.end();
				});
			} else {
				res.writeHead(405, {'Content-Type': 'text/plain'});
				res.end('405 Method Not Allowed');
			}

		} else { 
			var file = fs.createReadStream(realPath);
			res.writeHead(200, {'Content-Type': 'text/html'}); 
			file.on('data',	res.write.bind(res)); 
			file.on('close', res.end.bind(res));
			file.on('error', function(err){ 
				res.writeHead(500, {'Content-Type': 'text/plain'}); 
				res.end("500 Internal Server Error"); 
			}); 
		}
	});
});

function genKeys(post) {
	var software = post['software'].toUpperCase();
	var lsn =  post['lsn'].toUpperCase();
	var model = post['model'].toUpperCase();
	var days = post['days'];
	var month = post['month'];
	var day = post['day'];
	var year = post['year'];
	var key;
	var dateKey;
	
	if(software && software.length > 0) {
		var serialModel = padding_right(lsn, ' ', 10) + padding_right(model, ' ', 4);
		var code = keys.getCode(serialModel, software);
		key = keys.getKey(code);
		dateKey = keys.getTempKey(software, year, month, day, days);
	} 

	return [key, dateKey];
}

webserver.listen(port, ip); 
console.log('Server running at http://' + ip + ':' + port);

This is a little more involved than the simple example we did before that only would spit out the results of an SQL statement or CL command.  Instead, we're seeing how we can import pieces of code that may have been developed on another machine, even running on a different OS.  

This was done using the following line of code:

var keys = require('software');

If we remember, I had set up a directory to hold my node.js applications.  It was /nodejs/bvstone.  I also decided to set up a subdirectory named "node_modules" under this directory where I could place my node modules.  Think of these, if you're an RPG programmer, as a module or service program.  The require() function pulls in the functionality of that node module so it is available in our application.

Update:  On a whim I decided to try and test if I needed to specify the qualified path to the software.js module  It appears I don't!  This Article explains where node.js looks for modules used on the require() function.  So instead of specifying var keys = require('./node_modules/software');, I only need to specify var keys = require(''software');

The main functions we use are getCode() and getTempKey().  The first accepts a string which is the logical serial number and model number of the machine the key is for as well as the software.

The getTempKey() function accepts a software name, year, month, day and number of days to generate a temporary key for.  So, in our code we see that after we've determined there is a POST request being made, we call a function named genKeys() that is part of our main node source.  This code performs the following functions:

  • Reads in the form data from the POST (which is passed in as a parameter)
  • Concatenates the serial number and model number together into one string that will be used to pass into our getCode() function.  This is done using a home-built function that allows us to pad the values with spaces (or any character) so the string is formatted as 10 characters that contain the logical serial number followed by 4 characters that contains the model number.  
  • This value, along with the name of the software, is passed into the getCode() function.  Remember, this is brought in using the require() function pulling in the available functions from our software.js node module.  (I won't post this since it's proprietary code)
  • We then pas in the year, month, day, number of days and software name so we can generate temporary key if we need one.
  • Finally, we simply exit the getKeys() function and return an array.  The first element being the license key and the second element being the temporary date key and display these in the browser.

It was neat seeing how I could pull in a node module that I had created before without changing a single character and having it work just fine on a different piece of hardware and OS.


Last edited 03/29/2016 at 08:03:48


Reply




Copyright 1983-2017 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).