In the previous post I presented CobolScript generating output using templates. It can be used to generate web page output, too. The sample is at:
https://github.com/ajlopez/CobolScript/tree/master/samples/templateweb
The launch program in JavaScript is simple:
var cobs = require('../..'), http = require('http'), fs = require('fs'); var program = cobs.compileTemplateFile('./factorial.cobp'); http.createServer(function(req, res) { program.run(cobs.getRuntime({ request: req, response: res })); }).listen(8000); console.log('Server started, listening at port 8000');
The key part is the call to compile the file template. It produce a compiled JavaScript function to be invoked. The call of program.run executes the template, with a giving runtime context. The runtime context is build giving the request and response objects of the current incoming request. That runtime derives all the output of the CobolScript program to the response output. So, the template doesn’t know about web request and response. You can see the runtime object as a service provider to the compiled CobolScript program. Its properties can be accessed if you define a LINKAGE SECTION as in classic COBOL. But this feature was not used in this simple sample.
The template file
<h1>Factorial</h1> <p>Page generated by CobolScript, using templates</p> <table> <tr><th align='right'>n</th><th align='right'>n! </th> </tr> <# local n. perform show-factorial using n varying n from 1 to 10. #> </table> <# . stop run. show-factorial section using n. local result. perform factorial using n giving result. #> <tr> <td align='right'>${n}</td><td align='right'>${result} </td> </tr> <# . factorial section using n. local m. if n = 1 then return n. subtract 1 from n giving m. perform factorial using m giving m. multiply n by m. return m. #>
Launch the server with
node server
Navigate to localhost:8000, the result:
Next post: a dynamic web site accessing MySQL database, using CobolScript.
Keep tuned!
Angel “Java” Lopez
http://www.ajlopez.com