GromJS
Server-Side JavaScript interpreter
HOME | REFERENCE MANUAL | DOWNLOAD | EXAMPLE SCRIPTS | SUBMIT A COMMENT

Contents

  1. Description
  2. Examples
  3. Reference Manual
  4. Download

Description

GromJS is a Server-Side JavaScript (SSJS) interpreter that provides Web designers and programmers ability to use JavaScript object-based code for creating dynamic pages / scripts on Web server with same functionality as PHP, CGI, ASP, mod_perl or any other server side scripting solution.

It uses Mozilla's SpiderMonkey as the core script engine and includes persistent operation mode (executes multiple scripts by single process), session management, direct read/write access to local Web server files and directories, support for MySQL, PostgreSQL and SQLite databases, system program execution and pipes, HTTP file-upload and authentication, cookies and a full set of incoming CGI variables are available to the SSJS scripts.

Bauk/GromJS represents the most advanced SSJS system providing more functionality and features than any other SSJS platform!
For example it has more speed and features than MS IIS/JScript ASP scripting server. Check GromJS Reference Manual for detailed API specification of functions, classes and objects that GromJS interpreter includes.

Manage your work, MySQL database, automate jobs, backups, make utility and shell scripts, content management systems, forums, blogs, social Web sites, Online stores, shopping baskets, perform system administration, Web site administration, or anything else you can imagine using powerful JS code!!

JS API

In addition to standard JavaScript objects and classes such as Number, String, Object, Array, etc., GromJS Server-Side JavaScript interpreter provides following built-in JavaScript objects, classes and functions allowing JS manipulating data on Web server:

Classes:

  • File class (working with files on server, ie. open, close, read, write, lock, seek, tell, truncate, etc., see File class example below)
  • Mysql class (JS API for manipulating MySQL database, methods connect, close, query, exec, ping, selectdb, colnames, colcount, fetchrow, fetchallrows, insertid, affectedrows, error, errno, escapestring, realescapestring, freeresult, etc., see example below)
  • Postgresql class (JS API for manipulating PostgreSQL database, methods connect, close, query, exec, ping, colnames, colcount, fetchrow, fetchallrows, lastoid, affectedrows, error, escapestring, freeresult, etc.)
  • Sqlite class (JS API for manipulating SQLite database, methods open, close, exec, query, colname, colnames, coltype, colcount, rowcount, rowhas, fetchrow, fetchallrows, lastinsertrowid, rewind, error, freeresult, etc.)
  • Pipe class (executing external programs and communicating through Unix pipe)
  • Dictionary class (hash array, unlimited number of entries, configurable efficiency/speed)

Objects:

  • Cgi object (automatically handles input/client-sent data and provides functions for manipulation of this info ie. HTTP file-upload, data from GET/POST, cookies, setting response headers, etc. Example: var cdata = Cgi.getcookie("mycookie");), etc. see examples below
  • Server object (includes various utility functions ie. time, microtime, rand, etc. FS API ie. chmod, unlink, mkdir, rmdir, copy, etc. Example: Server.chmod("/path/to/file.txt", 0644); etc.
  • Session object (data persistence through multiple script executions)

Functions:

  • print() GromJS native ``print´´ function
  • include() Include script; allows modular development of complex/large JS apps

See the detailed info and definitions of built-in classes, objects and functions in GromJS Reference Manual.

Syntax highlighting by: JS Colorize jscolorize.jsx.


Examples

See Bauk Forum application (powered by GromJS/MySQL).
Below is a set of SSJS scripts that demonstrate JS scripting on Web server (copy/paste examples).

Environment variables

Sample script "env.jsx", see example scripts repository

//sample Server-Side JavaScript Web script
//print environment variables
print("<html><body>");
print("<h1>Environment variables</h1><hr>");
print("<pre>");

//print env variable
print(Server.getenv("HTTP_USER_AGENT"), "\n"); //Mozilla/5.0 (Windows NT 5.1; en-US)
print(Server.getenv("REMOTE_ADDR"), "\n");     //123.45.6.78

//get array of all environment variable names
var ENVNAMES 	= Server.varnames();
var cVarnam 	= 0;
//print all names in array, ie.
//REQUEST_METHOD = POST
//CONTENT_LENGTH = 123
//QUERY_STRING   = abc=234 //etc. etc.
for ( cVarnam=0; cVarnam<ENVNAMES.length; cVarnam++ ) {
	var evnam = ENVNAMES[cVarnam];
	print(evnam, " = ", Server.getenv(evnam), "\n");
	}
print("</pre>");
print("</body></html>");

File class

Sample script "file_class.jsx", see example scripts repository

//example Server-Side JavaScript Web script
//File class;
//Open file, lock, write text to file, then read and print data from file
//
//set response headers (optional)
Cgi.header("Content-Type: text/html; charset=ISO-8859-2");
Cgi.header("X-Powered-By: GromJS/1.7.9");

//output HTML page
print("<html><body>");

var fileobj = new File("myfile.txt"); //File class

//set errno 0
Server.errno(0);

//open for RW, truncate and if not existing create mode 0644
if ( fileobj.open("rwtc", 0644)==true ) {
	//set exclusive lock
	if ( fileobj.lock("x")==true ) {
		//write ok
		if ( fileobj.write("abc text text more text")!=(-1) ) {
			//rewind to beggining
			fileobj.seek(0);

			//read (with no arg) whole file
			var strfromfile = fileobj.read();

			//data from file successfully read
			if ( strfromfile!=null ) {
				print("Data read from file:<br>");
				print(strfromfile, "<br>");
				}
			}
		}
	//close file
	fileobj.close();
	}
//error opening file
else {
	//ie. invalid path, no permission etc.
	//Error open file [errno 2] No such file or directory
	print("Error open file: " , Server.errnstr() ,"<br\n>");
	}
print("</body></html>");

Cgi data

Sample script "cgi_data.jsx", see example scripts repository

//sample Server-Side JavaScript Web script
//handle user submitted information

//wrapper class
function Cgidata(snam)
	{
	this.n = snam;
	this.v = Cgi.any(snam,"");
	return ;
	}
//user submitted information
var UFirstname  = new Cgidata("firstname");
var USecondname = new Cgidata("secondname");
var UComment    = new Cgidata("comment");

//print HTML page
print("<html><body>");
print("<h1>User comments:</h1><hr>");

//user submitted information
if ( UFirstname.v!="" && USecondname.v!="" && UComment.v!="" ) {
	print("<p><b>Submitted:</b></p\n>");
	print("<p>First name: ", Server.htmlencode(UFirstname.v),"</p\n>");
	print("<p>Second name: ", Server.htmlencode(USecondname.v),"</p\n>");
	print("<p>Comment: ", Server.htmlencode(UComment.v),"</p\n>");
	}

//print HTML form
print("<form action=\"", Server.getenv("SCRIPT_NAME",""), "\" method=POST>");
print("First name:<br>");
print("<input type=text name=", UFirstname.n,
	" value=\"", Server.htmlencode(UFirstname.v), "\"><br>");
print("Second name:<br>");
print("<input type=text name=", USecondname.n,
	" value=\"", Server.htmlencode(USecondname.v), "\"><br>");
print("Comment:<br>");
print("<textarea name=", UComment.n, " rows=10 cols=30>");
print(Server.htmlencode(UComment.v));
print("</textarea><br>");
print("<input type=submit value=Submit>");
print("</form>");

print("</body></html>");

Upload script

Sample script "upload.jsx", see example scripts repository

/*
For reference of built-in objects, classes and functions available with GromJS
Server-Side JavaScript interpreter see online manual at GromJS homepage:
   http://www.bauk.ws/gromjs.html
*/
//example Server-Side JavaScript Web script
//demonstrate HTTP file upload
print("<html><body>");
print("<h1>File upload script</h1\n>");
print("Number of uploaded files: [", Cgi.Files.length, "] file(s)<br\n>");

//set errno 0
Server.errno(0);
//array non-empty if any files uploaded
if ( Cgi.Files.length  ) {
	/*
	Uploadedfile object
	properties:
	var Upfl = Cgi.Files[0];

	string Upfl.name;     //name of HTML form input field ie "myphoto1"
	string Upfl.filename; //filename on client's disk ie. "c:\files\myfile.gif"
	string Upfl.tmpfile;  //uploaded file ie. /tmp/upload23457742463624355325
	string Upfl.type;     //file type ie. image/gif
	string Upfl.encoding; //encoding ie.
	uint   Upfl.size;     //size of uploaded file in bytes
	*/
	// Uploadedfile object in Files array
	var Upfl = Cgi.Files[0];

	print("<div>Moving uploaded file into upload dir... ");
	//move tmp file
	if ( Server.move(Upfl.tmpfile, "upload/newfile.jpg")==true ) {
		print("[OK]");
		}
	else {
		//print error
		print("[error]<br>");
		print(Server.errnstr());
		}
	print("</div\n>");
	print("Done.<br\n>");
	}

//print file upload HTML form
print("<hr>");
print("<h2>Upload form:</h2>");

//point the form to script itself
print("<form action=", Server.getenv("SCRIPT_NAME",""),
	" method=POST enctype=\"multipart/form-data\">");
print("File:<br>");
print("<input type=\"file\" name=\"myphoto1\"><br><br>");
print("<input type=\"submit\" name=\"Submit\" value=\"Upload\">");
print("</form>");
print("</body></html>");

Mysql class

Sample script "mysql_class.jsx", see example scripts repository

/*
For reference of built-in objects, classes and functions available with GromJS
Server-Side JavaScript interpreter see online manual at GromJS homepage:
   http://www.bauk.ws/gromjs.html
*/

//example Server-Side JavaScript Web script
//demonstrate using MySQL database and JavaScript API
//BEGIN CODE-->
//Mysql class;
//Create new database and table, insert values, and then select and print
//values from table
print("<html><body>");

var dbobj = new Mysql(); //Mysql class

//set errno 0
Server.errno(0);

//connect to database server
if ( dbobj.connect("hostname.net","usernam","pswrd",3306,"/tmp/mysql.sock")==true ) {
	//create database, table, insert values:
	//dbobj.query("CREATE DATABASE testdb");
	//dbobj.selectdb("testdb");
	//dbobj.query("CREATE TABLE testtbl ...");
	//dbobj.query("INSERT INTO testtbl ..."); etc. etc.

	//query values:
	if ( dbobj.selectdb("testdb")==true ) {
		//now query
		if ( dbobj.query("SELECT id FROM testtbl")==true ) {
			var onerow;
			//get row as JS Array object from result set
			while ( (onerow=dbobj.rowfetch())!=null ) {
				//print column:
				//1 <br>
				//2 <br>
				print(onerow[0], "<br>");
				}
			//optional; results free'd automatically
			dbobj.freeresult();
			}
		}
	//error
	else {
		//ie. cannot select database etc.
		print("Error in exec, error [" ,dbobj.error(), "]<br>");
		}

	//close connection
	dbobj.close();
	}
//error
else {
	//ie. can not connect to database server etc.
	print("Error connecting to database, error [" ,dbobj.error(), "]<br>");
	//ie. no permission etc.
	print("System errno: " , Server.errnstr(), "<br>");
	}
print("</body></html>");

// <--END CODE

Postgresql class

Sample script "postgresql_class.jsx", see example scripts repository

//example Server-Side JavaScript Web script
//example:
/*Precondition: In order to use GromJS JavaScript with PostgreSQL database,
following conditions must be met:
   - PostgreSQL database server must be installed/started
   - valid PostgreSQL user (not system user), ie. "luka" must be created and
   this user must have privileges to use/manipulate database in desired way,
   consult PostgreSQL manual for details on administration.
   - valid PostgreSQL database must be created
For example, PostgreSQL server is started.
Create user "luka" and database "testdb", from command line:
   % /usr/local/pgsql/bin/createuser -P -e -d luka
   % /usr/local/pgsql/bin/createdb testdb
After these conditions are met, proceed with GromJS JavaScript, example code
below:
*/
//BEGIN CODE-->
//create Postgresql object
var pdbobj = new Postgresql();

print("<html><body>");
print("<h1>PostgreSQL database</h1><hr>");

//PostgreSQL connect() function accepts a single string with connection parameters
//in form of NAME=VAL, multiple arguments separated by space.
//Ie. main parameters are "dbname", "user" and "password", for full list of
//parameters consult PostgreSQL manual, topic client connection parameters
if ( pdbobj.connect("dbname=testdb user=luka password=lukaspswrd567")==true ) {
	print("<p>[ok connected]</p>");

	//SQL statement
	var sSqlstmt = "CREATE TABLE ttesttbl(  "
		+" rowid  integer PRIMARY KEY,  "
		+" smpl2  varchar(255),         "
		+" smpl3  varchar(255)          "
		+" );                           ";
	//execute SQL statement
	if ( pdbobj.exec(sSqlstmt)==true ) {
		print("<p>[ok created table]</p>");
		}
	else {
		print("<p>[error exec]</p>");
		print("<p>[",pdbobj.error(),"]</p>");
		}
	//insert values to database
	//pdbobj.exec("INSERT INTO ttesttbl ... ");
	//pdbobj.exec("INSERT INTO ttesttbl ... ");
	//etc etc..

	//execute SQL statement
	if ( pdbobj.query("SELECT rowid,smpl2,smpl3 FROM ttesttbl")==true ) {
		var onerow;
		while ( (onerow=pdbobj.fetchrow())!=null ) {
			//123 -- xyz -- abc<br>
			print(onerow[0], " -- ", onerow[1], " -- ",
				onerow[2], "<br\n>");
			}
		}
	else {
		print("<p>[error query]</p>");
		print("<p>[",pdbobj.error(),"]</p>");
		}


	pdbobj.close();
	}
else {
	print("<p>[error connect]</p>");
	print("<p>[",pdbobj.error(),"]</p>");
	}

print("</body></html>");
// <--END CODE

Sqlite class

Sample script "sqlite_class.jsx", see example scripts repository

//Sqlite class;
//Create new table in database file, insert values, and then select and print
//values from table
//BEGIN CODE-->
print("<html><body>");
print("<h1>SQLite database</h1>");

var dbfilepath 	= "mydatabase.db";        //database file path
var sdbobj 	= new Sqlite(dbfilepath); //Sqlite class

//set errno 0
Server.errno(0);

//open database file
if ( sdbobj.open()==true ) {
	//exec SQL statements
	if ( sdbobj.exec("CREATE TABLE ttesttable ... ; ")==true ) {
		//insert statements, ie.
		//sdbobj.exec("INSERT INTO ttesttable ... ;"); etc. etc.

		//now query
		if ( sdbobj.query("SELECT * FROM ttesttable")==true ) {
			var onerow;
			//get row as JS Array object from result set
			while ( (onerow=sdbobj.rowfetch())!=null ) {
				//print columns in form ie.
				//val1 val2 val3 ... <br>
				for ( indx=0; indx!=onerow.length; indx++ ) {
					print(onerow[indx], " ");
					}
				print("<br>");
				}
			}
		}
	//error
	else {
		//ie. cannot create table etc.
		print("Error in exec, error [" ,sdbobj.error(), "]<br>");
		}

	//close database
	sdbobj.close();
	}
//error
else {
	//ie. not a database file, etc.
	print("Error opening database, error [" ,sdbobj.error(), "]<br>");
	//ie. invalid path, no permission etc.
	print("System errno: " , Server.errnstr(), "<br>");
	}
print("</body></html>");
// <--END CODE

Sqlite wrapper

Sample script "sqlite_wrapper.jsx", see example scripts repository

//example Server-Side JavaScript Web script
//demonstrate using Sqlite3 database and JavaScript API
//BEGIN CODE-->
Sqlite.prototype.objtable = function(sSQLQuery)
	{
	var sdbobj = this;

	//class
	function Objarr()
		{
		var Objv         = this;
		var aNewrows     = new Array();

		Objv.aLargearr   = aNewrows;
		Objv.c           = 0;
		Objv.aColnames   = sdbobj.colnames();
		Objv.nCols       = Objv.aColnames.length;
		//tmp vars
		var aDBColnames  = Objv.aColnames;
		var nDBCols      = Objv.nCols;
		var aonerow      = null;

		while ((aonerow=sdbobj.fetchrow())!=null) {
			var cCols  = 0;
			var Row    = new Object();
			//obj["propnam"] = "val";
			while ( cCols<nDBCols ) {
				Row[aDBColnames[cCols]] = aonerow[cCols];
				cCols++;
				}

			aNewrows.push(Row);
			}

		Objv.nRows = Objv.aLargearr.length;


		//methods
		Objv.bottom = function()
			{
			Objv.c=0;
			return;
			}
		Objv.rownext = function()
			{
			Objv.c++;
			return;
			}
		Objv.rowhas = function()
			{
			return Objv.c<Objv.nRows;
			}
		Objv.rowget = function()
			{
			return Objv.aLargearr[Objv.c];
			}
		Objv.len = function()
			{
			return Objv.aLargearr.length;
			}
		Objv.fetchrow = function()
			{
			if ( Objv.c<Objv.nRows ) {
				Objv.c++;
				return Objv.aLargearr[Objv.c-1];
				}
			return null;
			}
		return;
		}
	//query
	if ( sdbobj.query(sSQLQuery)==true ) {
		return new Objarr(sdbobj);
		}

	print("<p>Sqlite [error: ", sdbobj.error(), "]</p>");
	print("<p>Error in query: ", sSQLQuery,  "</p>");
	Server.exit();
	return null;
	}


//Sqlite class
var sdbobj = new Sqlite("sampledb.db");

if ( sdbobj.open()==true ) {
	var TRows = sdbobj.objtable("SELECT rowid,aaa,bbb,cc FROM ttesttbl");
	var Row;

	while ( (Row=TRows.fetchrow())!=null )  {
		//1234 - aaa - bbb - cc<br>
		print(Row.rowid, " - ", Row.aaa, " - ", Row.bbb, " - ",
			Row.cc, "<br>");
		}
	sdbobj.close();
	}


// <--END CODE

Note: For full list of examples see GromJS Reference Manual with prototypes of Server-Side JavaScript built-in objects and classes ie. Server, Cgi, Session, File, Mysql, Postgresql, Sqlite, Dictionary, Pipe, etc. and additional Server-Side JavaScript example scripts.




Download

GromJS Version 1.7.11
OS support: Linux and *BSD systems
Package: Bauk/2.0.19 HTTP Server, Grom connector library, SpiderMonkey js-1.7.0 interpreter
For installation instructions see INSTALL file

Downloadgromjs-1.7.11.tgz | 2.50 MB | CURRENT








GromJS 1.7.11 JavaScript-C

Powered by Bauk. Copyright © 2009. www.bauk.ws