Example usage of teajs
Below are to very simple examples of teajs usage. We intentionally not covering here more or less complicated thing like request arguments and, files and other things.
CGI mode
//#!/usr/local/bin/teajs
// requiring modules
var PostgreSQL=require("pgsql").PostgreSQL;
// connecting to database and fetching data
var dburl="host=127.0.0.1 user=testuser password=testpass dbname=testdb";
var dbh=new PostgreSQL(dburl);
var l=request.get.letter||"";
var res=dbh.queryParams("select login from Tusers where login ilike $1",[l+"%"]);
var ret=res.fetchAllObjects();
// generating html
var html="";
html+="<html><body>\n";
html+="<h1>Logins starting with letter '"+l+"'</h1>\n";
html+="<ul>";
ret.forEach(function(obj) {
html+="<li>"+obj.login+"</li>\n";
});
html+="</ul>\n";
html+="</body></html>";
// sending html
response.status(200);
response.header({"Content-Type":"text/html"});
response.write(html);
FastCGI mode
//#!/usr/local/bin/teajs
// requiring modules
var PostgreSQL=require("pgsql").PostgreSQL;
// connecting to database and fetching data
var dburl="host=127.0.0.1 user=testuser password=testpass dbname=testdb";
var dbh=new PostgreSQL(dburl);
var force_exit;
while (!force_exit && system.FCGI_Accept()>=0) {
var l=request.get.letter||"";
var res=dbh.queryParams("select login from Tusers where login ilike $1",[l+"%"]);
var ret=res.fetchAllObjects();
// generating html
var html="";
html+="<html><body>\n";
html+="<h1>Logins starting with letter '"+l+"'</h1>\n";
html+="<p>letters: ";
for (var i=97;i<123;i++) {
html+="<a href='/?letter="+String.fromCharCode(i)+"'>"+String.fromCharCode(i)+"</a> ";
}
ret.forEach(function(obj) {
html+="<li>"+obj.login+"</li>\n";
});
html+="</ul>\n";
html+="</body></html>";
// sending html
response.status(200);
response.header({"Content-Type":"text/html"});
response.write(html);
}