JavaScript Server Pages

From Lianjapedia
Jump to: navigation, search

Lianja Cloud Server has built-in support for JavaScript Server Pages.

JavaScript Server Pages (.jssp pages) are modeled on PHP Server Pages. The Lianja Cloud Server embeds the V8 JavaScript engine which is used by Google Chrome and Node.js. The JavaScript engine implements ECMAScript as specified in ECMA-262, 5th edition.

Lianja JavaScript Server Pages provide the ability to write both the client and the server code in JavaScript. You can use them in place of PHP server pages. If you know JavaScript and HTML these are for you. You are not required to learn any new programming language in order to build custom Web and Mobile Apps.

JavaScript Server Pages have embedded Lianja database and OData support and they are fast as the whole engine is written in C/C++, no Java or other dependencies required.

Additionally they support "one-time" loading of JavaScript libraries to enable team sharing of code libraries and open source JavaScript libraries using the built-in include_once("library_name.js") and the require("module_name") function.

They also provide a lot of functionality that will be familiar to PHP developers (details below).

You can write and test JavaScript Server Pages in the Lianja App Builder and "Publish to the Cloud website" to deploy them.

Page processing directives

Page Directive Description
<%= expression %> Evaluates and outputs expression.
<% code %> Executes code.
<%@ include=cExpression %> Includes the file specified in cExpression.
<%@ codebehind=cExpression %> Executes the script file specified in cExpression.

When a JavaScript Server Page is executed the following are available to the script (in addition to the built-in ECMAScript JavaScript functions). These should be familiar to PHP Web developers as they use the same naming convention.

Global associative arrays(objects)

Array Description
_ARGS[] Contains all arguments passed to the jssp page.
_COOKIE[] Contains cookie names and values.
_FILES[] Contains information about uploaded files.
_GET[] Contains GET parameter names and values.
_POST[] Contains POST parameter names and values.
_REQUEST[] Contains all arguments passed to the jssp page.
_SERVER[] Contains server information.
_SESSION[] Contains session information.

Global objects

Lianja

Method Description
Lianja.evaluate( vfp_expression ) Evaluates vfp_expression and returns the result.
Lianja.execute( vfp_command ) Executes vfp_command.
Lianja.openDatabase( databasename ) Opens the database databasename and returns an object reference.

Database

Method Description
close() Closes the database.
openRecordSet( table_or_sqlselect ) Opens a recordset for table_or_sqlselect and returns an object reference.

Recordset

Property Type Description
bof Boolean (R) Whether record pointer is at beginning of the recordset.
eof Boolean (R) Whether record pointer is at end of the recordset.
fieldcount Numeric (R) Number of fields in the recordset.
filter Character (RW) Filter expression to restrict records.
found Boolean (R) Whether the last seek or find operation found a match.
index Character (RW) Name of the index tag.
nomatch Boolean (R) Whether the last seek or find operation did not find any matches.
recno Numeric (R) Current record number in the recordset.
reccount Numeric (R) Number of records in the recordset.


Method Description
close() Closes the recordset.
delete() Marks the current record in a table recordset for deletion.
edit() Puts the current record in edit mode prior to field value modification. Complete the modification by calling update().
field( name_or_number ) Returns an object reference to the specified field in the current record.
findFirst( condition ) Moves the record pointer to the first record to match the condition.
findLast( condition ) Moves the record pointer to the last record to match the condition.
findNext( condition ) Moves the record pointer to the next record to match the condition.
findPrevious( condition ) Moves the record pointer to the previous record to match the condition.
move( numrecords, numbookmark ) Moves the record pointer by numrecords number of records from the numbookmark position.
moveBookmark( numbookmark ) Moves bookmark to the specified record number numbookmark in the recordset.
moveFirst() Moves the record pointer to the first record in the recordset.
moveLast() Moves the record pointer to the last record in the recordset.
moveNext() Moves the record pointer to the next record in the recordset.
movePrevious() Moves the record pointer to the previous record in the recordset.
moveRelative( numrecords ) Moves the record pointer by numrecords number of records.
requery() Requeries the recordset.
update() Completes an edit() method operation.

Field

Property Type Description
decimals Numeric (R) Field decimal places.
name Character (R) Field name.
type Numeric (R) Field data type.
value Expr (RW) Field value.
width Numeric (R) Field width.

Response

Method Description
Response.addCookie(name, value) Sends specified cookie.
Response.addHeader(name, value) Sends specified response headers.
Response.appendToLog( message ) Writes message to the end of the system log.
Response.authenticate( [message] ) Causes the browser to prompt for username/password with optional message.
Response.clear() Clears the output buffer.
Response.flush() Flushes out the buffers and sends to the browser.
Response.include( filename ) Includes filename.
Response.redirect( url ) Redirects to url.
Response.write( text ) Writes text.
Response.writeFile( filename ) Writes out the contents of filename.

Built-in commands/functions

Function Description
base64_encode_image(object_fieldname [,width,height [,cssattr ]]) Generates the base64 encoded contents of an object field image as an IMG tag with the specified dimensions and outputs into the HTML5. "cssattr" can be used to add additional CSS attributes onto the IMG tag e.g. "class='myclass'".
die(message) Terminates the session and displays the HTML encoded message.
debugout(message) Writes message to the debug output file.
echo( cExpression ) Evaluates and outputs cExpression.
getParameter(param, defaultvalue) Returns a named parameter from the request.
include( filename ) Includes the file contents specified by filename at this point in the script.
include_once( filename ) Includes the file contents specified by filename at this point in the script only once.
move_uploaded_file(tofilename, fromfilename) Moves the uploaded file tofilename to the location specified in fromfilename.
objectRead( from_filename, object_fieldname ) Reads the data from the from_filename file into the field object_fieldname.
objectType( object_fieldname ) Returns the type (file extension) of the field object_fieldname.
objectWrite( to_filename, object_fieldname ) Writes the data from the field object_fieldname into the file to_filename.
odata_Create( url, jsondatastring ) Inserts a new record described by the specified OData url whose field names and values are in the specified JSON encoded string e.g. '{"name":"smith", "salary":1000}'
odata_Delete( url, jsondatastring ) Deletes the record described by the specified OData url whose field names and values are in the specified JSON encoded string e.g. '{"name":"smith"}'
odata_Read( url [, filename] ) Queries data based on the specified OData url and sends the JSON encoded data back to the browser. If "filename" is specified the JSON data is saved into the specified file.
odata_Update( url, jsondatastring ) Updates a record described by the specified OData url with field names and values as specified by the JSON encoded string e.g. '{"name":"smith", "salary":1000}'
print( cExpression ) Evaluates and outputs cExpression.
require( cExpression ) Loads the JavaScript library module file specified in cExpression and returns back the module.exports object. This is used in a similar way to node.js require() e.g.
var utils = require("./utils");

where ./utils.js contains:

var myfunc = function() {
    // myfunc code
};
var myfunc2 = function() {
    // myfunc2 code
};
module.exports.myfunc = myfunc;
module.exports.myfunc2 = myfunc2;
tmpnam() Returns a temporary file name.
unlink( filename ) Deletes the file filename.
All of the VFP commands and functions supported by Lianja Using:

Lianja.execute( vfp_command )
result = Lianja.evaluate( vfp_expression )

View an example JavaScript Server Page. Further examples are included in the Lianja Cloud Server distribution in the C:\lianja\cloudserver\tenants\public\wwwroot\examples folder (Windows) and /opt/lianja/cloudserver/tenants/public/wwwroot/examples directory (Linux).