GromJS Server-Side JavaScript interpreter |
| BAUK HOME | GROMJS HOME | REFERENCE MANUAL | DOWNLOAD | EXAMPLE SCRIPTS | BDB DATABASE | SUBMIT A COMMENT | SUPPORT |
JS APIIn 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. ExamplesSee Bauk Forum application (powered by Bauk/GromJS/MySQL). On-The-Fly Syntax Highlighting by JS Colorize jscolorize.jsx, developed by GromJS Team. Hello WorldSample script "helloworld.jsx", see example scripts repository. //Simplest Server-Side JavaScript Web script print("<html><body>"); print("<p>Hello, world.</p>"); print("</body></html>"); 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 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 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>"); Bdb classSample script "bdb_class.jsx", see example scripts repository. BDB is extreme performance database which provides client/server architecture, simple administration and user management, BDB 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: //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 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 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 classSample script "postgresql_class.jsx", see example scripts repository. /* After 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 */ //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 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 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. DownloadGromJS Version 1.7.15 |
GromJS 1.7.14 JavaScript-C Powered by Bauk. Copyright © 2011. www.bauk.ws |