GromJS Server-Side JavaScript interpreter |
| HOME | REFERENCE MANUAL | DOWNLOAD | EXAMPLE SCRIPTS | SUBMIT A COMMENT |
JS APIIn 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:
Objects:
Functions:
See the detailed info and definitions of built-in classes, objects and functions in GromJS Reference Manual. Syntax highlighting by: JS Colorize jscolorize.jsx. ExamplesSee Bauk Forum application (powered by GromJS/MySQL). Environment variablesSample 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 classSample 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 dataSample 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 scriptSample 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 classSample 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 classSample 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 classSample 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 wrapperSample 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. DownloadGromJS Version 1.7.11 |
GromJS 1.7.11 JavaScript-C Powered by Bauk. Copyright © 2009. www.bauk.ws |