B4J Tutorial [ABMaterial] B4JS - 04 Running Javascript on the server side (mini NodeJS?)

See for a B4JS introduction: https://www.b4x.com/android/forum/threads/abmaterial-b4js-0-9.90249/

Note: for this tutorial, I may use some typical ABM things. You can ignore them as they are just to show the result of the code in this tutorial.
-------------------------------------------------------------------------------

As we have seen in our previous tutorial, we can call B4JS subs and Javascript subs from withing our B4JS class and also from within our ABM page class.

But what if we could call our javascript validateCCNum from our B4J server, even if no browser is connected to our server?

To do this, me must first 'load' our B4JS classes into our B4J app. They are loaded onto our server.
This code is placed in our Main AppStart() method.
B4X:
' we are loading our B4JS.  In the final parameter, we can even load other .js files: Array as String("myJS1.js", "myJS2.js")
   ABM.B4JSLoadOnServer(File.DirApp & "/www/js/", Null)

Note 1: we are NOT using a browser on our server side.
Note 2: this does also mean that this engine can NOT use any DOM javascript code!

First method: we are calling a pure Javascript method (validateCCNum):
B4X:
   Dim CardNumber As String = "5105105105105100"
   
   Dim myB4JClassVar As B4JSServerVariable
   ' a pure javascript function does not belong to a B4JS class, so we pass an empty string
   myB4JClassVar.Initialize("myVar", "")
   
   Dim isValid As Boolean = myB4JClassVar.B4JSRunMethod("validateCCNum", Array As Object(CardNumber))
   If isValid Then
       Log("Server Card '" & CardNumber & "' is a valid card. Please continue...")
   Else
       Log("Server Card '" & CardNumber & "' is NOT valid. Please check the number...")
   End If

Second method: we are calling our own B4JS sub CheckCard:
B4X:
Dim myB4JClassVar2 As B4JSServerVariable
   ' Our CheckCard method DOES belong to our B4JSCalculateDistance class, so pass it
   myB4JClassVar2.Initialize("myVar2", "B4JSCalculateDistance")
   
   myB4JClassVar2.B4JSRunMethod("CheckCard", Array As Object(CardNumber))

The result (all in the B4J log of course as we do not have a browser):
B4X:
' from method 1
Server Card '5105105105105100' is a valid card. Please continue...
' from method 2
Card '5105105105105100' is a valid card. Please continue...

So what happens here? We create a new calculator variable: B4JSServerVariable
This variable is aware of all the B4JS/Javascript we have in our app (because of the ABM.B4JSLoadOnServer method).

Now we can use this variable to run a javascript method (a native one, or one of our own written in B4JS). For the latter, we do need to specify the B4JS class the method is defined in.

This system could have great potential in the future. Why not acting as a mini NodeJS server... But we'll see! ;)

This concludes this tutorial

Alwaysbusy
 

OliverA

Expert
Licensed User
Longtime User

OliverA

Expert
Licensed User
Longtime User

OliverA

Expert
Licensed User
Longtime User
And I'm really sad that node.js is such a force and seemed to have killed off Jaxer (http://www.jaxer.org/). With Jaxer, the SAME code could be used on both the client and the server (you could specify which code can run where: client, server, or both). I'm not an expert in either servers, but I can't seem to find the same ease of that functionality with node.js. Yeah, getting off topic a bit here, but it's funny that the same day I'm looking into this (server/client side JavaScript) stuff, this thread gets posted.
 
Top