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

Discussion in 'B4J Tutorials' started by alwaysbusy, Mar 6, 2018.

  1. alwaysbusy

    alwaysbusy Expert Licensed User

    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.
    ' 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):
    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
    Dim isValid As Boolean = myB4JClassVar.B4JSRunMethod("validateCCNum"Array As Object(CardNumber))
    If isValid Then
    Log("Server Card '" & CardNumber & "' is a valid card. Please continue...")
    Log("Server Card '" & CardNumber & "' is NOT valid. Please check the number...")
    End If
    Second method: we are calling our own B4JS sub CheckCard:
    Dim myB4JClassVar2 As B4JSServerVariable
    ' Our CheckCard method DOES belong to our B4JSCalculateDistance class, so pass it
    "CheckCard"Array As Object(CardNumber))
    The result (all in the B4J log of course as we do not have a browser):
    ' 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

    jinyistudio, magi6162, maXim and 5 others like this.
  2. OliverA

    OliverA Expert Licensed User

  3. alwaysbusy

    alwaysbusy Expert Licensed User

    I'm not using NodeJS (but you may have given me an idea for some future development).

    I'm using the java ScriptEngine 'nashorn' to evaluate the Javascript.
    jinyistudio, BPak and Don Oso like this.
  4. OliverA

    OliverA Expert Licensed User

    alwaysbusy likes this.
  5. OliverA

    OliverA Expert Licensed 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.
    jinyistudio likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice