Sign In / Register

Contents

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

Reference Manual | Download | Example Scripts | BDB Database | Support

GromJS
Server-Side JavaScript Interpreter

Description

GromJS is a Server-Side JavaScript interpreter based on Mozilla's SpiderMonkey core script engine which in operation with Bauk HTTP Server provides complete ASP-like environment for JavaScript scripting on Web server.
GromJS includes persistent operation mode (executes multiple scripts by single process for extreme performance), session management, direct read/write access to Web server files and directories, support for MySQL, PostgreSQL and SQLite databases, system program execution and pipes, HTTP file upload and authentication, cookies, CGI environment variables, and many more.

Bauk/GromJS represents the most advanced Server-Side JavaScript platform available in the world today, providing more functionality and features than any other JavaScript system. For example, it includes more features and higher performance than Microsoft's IIS / ASP JScript scripting server. See example scripts below on this page and GromJS reference manual for list of available features. This page you are viewing, and entire Bauk HTTP Server site, is powered by Bauk/GromJS. See other sites powered by Bauk/GromJS.

History: Developed in 2005. by Bauk HTTP Server Team.

Features

  • Based on Mozilla ´SpiderMonkey´ JavaScript core script engine
  • ASP-like JavaScript scripting environment — Ie. easy like working with HTML files, scripts (ie. .js .jsp .jsx .jss .sjs) placed in Web directory automatically executed by Web server/interpreter
  • Persistent operation mode — Single gromjs process/instance executes multiple JS scripts providing extreme performance
  • Single C executable ´gromjs´, low resource consumption
  • Database support (MySQL, PostgreSQL, SQLite)
  • Working with files / directories / file system
  • Mature/reliable code base
  • Portable — support for Linux, *BSD and all POSIX-compliant operating systems
  • It's absolutely FREE!

Development in 2012.

BDB Database

Recognizing the need for fast, universal and portable database in Web scripting sphere, GromJS Team as answer to this requirement developed BDB Database. BDB Database server is a standalone program ie. like MySQL and it comes included with GromJS Package. BDB Client library comes included by default with GromJS interpreter (ie. requires no linking) and provides JavaScript API for interacting with BDB server - See below Bulletin Board example script.

BDB Database is full featured database system and it includes client/server architecture, simple and easy administration and user management, a command language similar to SQL and ability to efficiently store large amounts of data.
Add/manage thousands of users, craft tables, columns and data, create large online JavaScript apps powered by database without consuming resources, all using GromJS/BDB Database.

Below on this page see example BDB script with Bulletin Board example script which demonstrates practical usage, or Bauk Forum application (powered by Bauk/GromJS/BDB), a more complex variant used in real world situation, and more info on BDB Database.

Applications with GromJS

Manage your work, MySQL/PostgreSQL/SQLite database, automate jobs, backups, make utility and shell scripts, content management systems, perform system administration, Web site administration, package build tools, have fun, or do anything else you imagine using JavaScript code on Web server.

GromJS JavaScript API

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

These include objects Cgi, Server and Session, classes File, Mysql, Postgresql, Sqlite, Pipe and Dictionary, functions print() and include().

See detailed info and definitions of built-in classes, objects and functions in GromJS Reference Manual and more examples on this page.

Examples

Below is a set of scripts that demonstrate JS scripting on Web server (copy/paste examples).

On-The-Fly Syntax Highlighting by JS Colorize jscolorize.jsp, developed by GromJS Team.

Hello World

Sample script "helloworld.jsp", see example scripts repository.

//Example Server-Side JavaScript Web script
print("<html><body>");
print("<p>Hello, world.</p>");
print("</body></html>");

Environment variables

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

//Example 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.jsp", 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 optional response headers
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("text to write")!=(-1) ) {
			//rewind to beggining
			fileobj.seek(0);

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

			//data from file successfully read
			if ( sfiledata!=null ) {
				print("Data read from file:<br>");
				print(sfiledata, "<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.jsp", see example scripts repository.

//Example 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.jsp", 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>");

Bdb class

Sample script "bdb_class.jsp", see example scripts repository.

BDB is extreme performance database which provides client/server architecture, simple administration and user management, a command language similar to SQL and ability to efficiently store large amounts of data. For specification and BDB syntax see GromJS Reference Manual topic ``Bdb class´´.

//Example Server-Side JavaScript Web script
//BEGIN SCRIPT-->
//A simple bulletin board script with BDB database
print("<html><body>");
print("<h1>BDB Bulletin Board</h1>");
print("<hr>");
//Wrapper method for handling rows and columns in objective style
Bdbclient.prototype.rowxobjectlist2 = function(sCmnd)
	{
	//Local class
	function ObjectListClass(aRowObjs)
		{
		var Lst    = this;     //List
		Lst.aROL   = aRowObjs; //Row object list
		Lst.nRows  = aRowObjs.length;
		Lst.cRow   = 0;
		//methods
		Lst.count = function() //Number of rows in list
			{
			return Lst.nRows;
			}
		Lst.next = function() //Fetch next row
			{
			if ( Lst.cRow<Lst.nRows ) {
				Lst.cRow++;
				return Lst.aROL[Lst.cRow-1];
				}
			return null;
			}
		return;
		}
	var Bdbv = this; //BDB database client
	if ( Bdbv.exec(sCmnd)==true ) {
		var aRowObjs = Bdbv.rowobjectlist(); //Get array of row objects
		if ( aRowObjs!=null ) {
			return (new ObjectListClass(aRowObjs));
			}
		}
	return null; //error
	}
//Encode
function HTMLENCODE(s)
	{
	return Server.htmlencode(s);
	}

//BDB database client
var Bdbc = new Bdbclient();
//Connect to BDB database server
if ( Bdbc.connect("localhost", 3347, "usrnam456", "pswrd678")==true ) {
	//Create a bboard database
	Bdbc.createdb("bboard");
	//Select database
	Bdbc.selectdb("bboard");

	//For BDB language syntax see GromJS Reference Manual
	//Create tables
	Bdbc.exec("CREATETABLE tthreads"
		+" STRING8 suser,"
		+" STRING8 stitle,"
		+" UINT32  t;"
		);
	Bdbc.exec("CREATETABLE tmessages"
		+" STRING8  suser,"
		+" STRING32 smessage,"
		+" UINT32   cthread,"
		+" UINT32   t;"
		);
	//Insert
	var sCmnd 	= "INSERT tthreads"
			+" suser='Peter'"
			+", stitle='Thread title'"
			+", t="+Server.time()+";";
	if ( Bdbc.exec(sCmnd)==true ) {
		var xRowid = Bdbc.lastinsertrowid(); //New thread rowid
		Bdbc.exec("INSERT tmessages"
			+" suser='Peter'"
			+", smessage='Message text'"
			+", cthread="+xRowid
			+", t="+Server.time()+";"
			);
		}

	//Select threads
	var Thrdlst = Bdbc.rowxobjectlist2("SELECT tthreads * ORDERBY t;");
	if ( Thrdlst!=null ) {
		var Thrd; //One thread
		while ( (Thrd=Thrdlst.next())!=null ) {
			//Select messages for this thread
			var sSelmsgcmnd = "SELECT tmessages * WHERE cthread=="
					+Thrd._rowid+" ORDERBY t;";
			var Msglst      = Bdbc.rowxobjectlist2(sSelmsgcmnd);
			//Print title
			print("<h2>",HTMLENCODE(Thrd._stitle),"</h2>");
			print("<p>Thread by: ",HTMLENCODE(Thrd._suser),"</p>");
			if ( Msglst!=null ) {
				var Msg; //One message
				//Print messages in a loop
				while ( (Msg=Msglst.next())!=null ) {
					//Print messages
					print("<p>By: ",
						HTMLENCODE(Msg._suser),
						"<br>",
						HTMLENCODE(Msg._smessage),
						"</p>"
						);
					}
				}
			}
		}
	//Close connection
	Bdbc.close();
	}
//Error
else {
	print("<p>[error] ",Bdbc.error(),"</p>");
	}
print("</body></html>");
//<--END SCRIPT

Mysql class

Sample script "mysql_class.jsp", 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 Mdb = new Mysql(); //Mysql class

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

//Connect to MySQL server
if ( Mdb.connect("domain.com", "usernam345", "pswrd345", 3306, "/tmp/mysql.sock")==true ) {
	//create database, table, insert values...
	//Mdb.query("CREATE DATABASE testdb");
	//Mdb.selectdb("testdb");
	//Mdb.query("CREATE TABLE testtbl ...");
	//Mdb.query("INSERT INTO testtbl ...");

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

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

Postgresql class

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

/*
After PostgreSQL server is installed and started, create user "luka" and
database "testdb", ie. from command line:
$ /usr/local/pgsql/bin/createuser -P -e -d luka
$ /usr/local/pgsql/bin/createdb testdb
*/
//Example Server-Side JavaScript Web script
//BEGIN CODE-->
//create Postgresql object
var Pgdb = new Postgresql();

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

//Connect to PostgreSQL server
if ( Pgdb.connect("dbname=testdb user=luka password=lukaspswrd567")==true ) {
	print("<p>Ok, connected to PostgreSQL server</p>");

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

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

Sqlite class

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

//Example Server-Side JavaScript Web script
//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 Sqdb 	= new Sqlite(dbfilepath); //Sqlite class

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

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

		//now query
		if ( Sqdb.query("SELECT * FROM ttesttable")==true ) {
			var onerow;
			//get row as JS Array object from result set
			while ( (onerow=Sqdb.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 [" ,Sqdb.error(), "]<br>");
		}

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

Get latest version of GromJS from download section.
OS support: Linux and BSD systems
Package: Bauk HTTP Server, GromJS / SpiderMonkey JS interpreter
For installation instructions see INSTALL file